Bonjour,

Je suis confronté aujourd'hui à un problème lors de l'ouverture d'une connexion avec la méthode Open().

En effet, j'ai un message d'erreur disant que la propriété connectionString n'est pas initialisée.

Je n'ai pas trouvé la solution à mon problème, et les problèmes similaires que j'ai cherché sur ce forum portent sur d'autres fonctions.

Ce que je souhaite faire dans mon code : au moment du clic sur le bouton btnEnvoyer, j'importe le contenu de mon fichier Excel dans une table SQL Server.

Voici le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
 
namespace MySolution.MyFirstWebPart
{
    [ToolboxItemAttribute(false)]
    public partial class MyFirstWebPart : WebPart
    {    
        public MyFirstWebPart()
        {
        }
 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
        }
 
        protected void btn1_Click(object sender, EventArgs e)
        {
            lbl1.Text = txtBox1.Text;
            txtBox1.Text = "";
        }
 
        protected void btnEnvoyer_Click(object sender, EventArgs e)
        {
            string excelPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Files/");
            FileUpload1.SaveAs(excelPath);
 
            string conString = string.Empty;
            string extension = Path.GetExtension(FileUpload1.FileName);
            switch (extension)
            {
                case ".xls": //Excel 97-03
                    conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                    break;
                case ".xlsx": //Excel 07 or higher
                    conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
                    break;
            }          
 
            conString = string.Format(conString, excelPath);
            using (OleDbConnection excel_con = new OleDbConnection(conString))
            {
                excel_con.Open(); // Le problème est ici
                string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
                DataTable dtExcelData = new DataTable();
 
                //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
                dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("CustomerId", typeof(int)),
                new DataColumn("Name", typeof(string)),
                new DataColumn("Country",typeof(string)) });
 
                using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
                {
                    oda.Fill(dtExcelData);
                }
                excel_con.Close();
 
                string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(consString))
                {
                    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                    {
                        //Set the database table name
                        sqlBulkCopy.DestinationTableName = "dbo.Customers";
 
                        //[OPTIONAL]: Map the Excel columns with that of the database table
                        sqlBulkCopy.ColumnMappings.Add("CustomerId", "CustomerId");
                        sqlBulkCopy.ColumnMappings.Add("Name", "Name");
                        sqlBulkCopy.ColumnMappings.Add("Country", "Country");
                        con.Open();
                        sqlBulkCopy.WriteToServer(dtExcelData);
                        con.Close();
                    }
                }
            }
        }
    }
}
Je continue mes recherches dans l'attente de vos réponses.
Merci.

Strix