Bonjour tt le monde,

Je suis débutante en c# et j'utilise Visual Studio 2010, et j'ai une application winform où je charge mes données à partir d'un fichier excel dans un datagridview (Etape que j'ai réussi à faire). Maintenant, je veux enregistrer ces données dans une table de ma base de données sql server (que j'ai créer sur Visual Studio déjà). Le problème que j'ai pas su comment faire cette deuxième étape. Est-ce que vous pouvez m'aider SVP? Je serai très reconnaissante.

Ici, je vous mets le code avec lequel j'ai remplis mon datagridview:
Code de chargement des données :
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
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
 
namespace projetBE
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
 
            InitializeComponent();
        }
 
        private void openFileButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
 
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string filePath = openFileDialog1.FileName;
            string extension = Path.GetExtension(filePath);
            string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
            string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
            string header = rbHeaderYes.Checked ? "YES" : "NO";
            string conStr;
 
            conStr = string.Empty;
            switch (extension)
            {
 
                case ".xls": //Excel 97-03
                    conStr = string.Format(Excel03ConString, filePath, header);
                    break;
 
                case ".xlsx": //Excel 07
                    conStr = string.Format(Excel07ConString, filePath, header);
                    break;
            }
 
            //Get all worksheet names from the Excel file selected using GetSchema of an OleDbConnection
            OleDbConnection connection = new OleDbConnection(conStr);
            connection.Open();
            DataTable tables = connection.GetSchema("Tables", new String[] { null, null, null, "TABLE" });
            connection.Dispose();
 
           //Add each table name to the combo box
           if (tables != null && tables.Rows.Count > 0)
           {
              worksheetsComboBox.Items.Clear();
              foreach (DataRow row in tables.Rows)
              {
                worksheetsComboBox.Items.Add(row["TABLE_NAME"].ToString());
              }
           }
           }
 
        private void worksheetsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Display the data from the selected Worksheet
            string filePath = openFileDialog1.FileName;
            string extension = Path.GetExtension(filePath);
            string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
            string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
            string header = rbHeaderYes.Checked ? "YES" : "NO";
            string conStr;
 
            conStr = string.Empty;
            switch (extension)
            {
 
                case ".xls": //Excel 97-03
                    conStr = string.Format(Excel03ConString, filePath, header);
                    break;
 
                case ".xlsx": //Excel 07
                    conStr = string.Format(Excel07ConString, filePath, header);
                    break;
            }
 
            OleDbDataAdapter adapter = new OleDbDataAdapter(String.Format("SELECT * FROM [{0}]", worksheetsComboBox.SelectedItem.ToString()), conStr);
            DataTable currentSheet = new DataTable();
            adapter.Fill(currentSheet);
            adapter.Dispose();
 
            excelDataGridView.DataSource = currentSheet;
 
        }
 
        }
    }
---------
J'explique un peu le code:
A chaque fois je charge mon fichier excel ce code me permet de récupérer les noms de mes feuilles excel et de parcourir ces différentes feuilles via un Combo Box et avec un click sur le nom de la feuille j'obtient mes données sur le datafridview.

Voilà mon pb, j'éspère que vous pouvez m'aider.

Merci d'avance.

Cordialement,