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
| private void button1_Click(object sender, EventArgs e)
{
// Déclaration du chemin du fichier source
string strDataSource = "C:\\Users\\raa\\Desktop\\CapitalStructureAddIn\\CSAddin_ClasseurDB_Test.xlsx";
// Déclaration du chemin de connexion
string ConnectionString = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source ={0}; Extended Properties =\"Excel 12.0;HDR=YES;\"", strDataSource);
// Déclaration d'une connexion au fichier source sur la base du chemin de connexion
// L'objet OleDbConnection représente une connexion unique à une source de donnée
OleDbConnection xlCon = new OleDbConnection(ConnectionString);
// Ouverture de la connexion
xlCon.Open();
// Création d'un DataAdapter pour prendre les données de l'onglet défini
// Le signe '$' après le nom du sheet indique que celui-ci existe déjà
OleDbDataAdapter xlData = new OleDbDataAdapter("Select * From [CSx_StructureGroups$]", xlCon);
// Déclaration et identification de la table à remplir dans le DataSet
DataTable tblTarget = new DataTable("tbl_StructureGroups");
// Remplir la table avec les données du OleDataAdapter
xlData.Fill(tblTarget);
DataRow drCurrent = drCurrent = tblTarget.NewRow();
drCurrent["StructureGroup_ID"] = null;
drCurrent["StructureGroup_Name"] = textBox1.Text;
drCurrent["Start_Date"] = 222222;
drCurrent["Start_Amount"] = 33;
drCurrent["Start_Structure"] = 3567;
tblTarget.Rows.Add(drCurrent);
dataGridView1.DataSource = tblTarget;
//Insert Command
xlData.InsertCommand = new OleDbCommand("INSERT INTO [CSx_StructureGroups$] (StructureGroup_ID, StructureGroup_Name, Start_Date, Start_Amount, Start_Structure) VALUES (? , ?, ?, ?, ?)", xlCon);
xlData.InsertCommand.Parameters.Add("@StructureGroup_ID", OleDbType.Integer).SourceColumn = "StructureGroup_ID";
xlData.InsertCommand.Parameters.Add("@StructureGroup_Name", OleDbType.Double).SourceColumn = "StructureGroup_Name";
xlData.InsertCommand.Parameters.Add("@Start_Date", OleDbType.Double).SourceColumn = "Start_Date";
xlData.InsertCommand.Parameters.Add("@Start_Amount", OleDbType.Double).SourceColumn = "Start_Amount";
xlData.InsertCommand.Parameters.Add("@Start_Structure", OleDbType.Double).SourceColumn = "Start_Structure";
xlData.Update(tblTarget);
xlCon.Close();
} |
Partager