Bonsoir,

Je suis entrain de développer une application WPF. J'utilise Entity Framework code first pour créer ma base de données de type One to Many.
Ensuite j'affiche le contenu de ma base dans deux datagrid. Pour enrichir ma base j'importe des données depuis un fichier Excel. j'arrive à faire l'import mais ensuite je ne sais pas comment faire pour sauvegarder ces données dans ma base.

Voici mon 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
        private void ImportCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var matrixOccupanciesViewSource = ((CollectionViewSource)(this.FindResource("matrixOccupanciesViewSource")));
 
 
            OpenFileDialog openDialog = new OpenFileDialog();
 
            // Show open file dialog box
            Nullable<bool> result = openDialog.ShowDialog();
 
            if (result == true)
            {
                string path = System.IO.Path.GetFullPath(openDialog.FileName);
                string query = "SELECT * FROM [A$]";
 
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                @"Data Source=" + path +";" +
                "Extended Properties=Excel 8.0;";
 
                OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
                DataTable data = new DataTable();
 
                adapter.Fill(data);
 
 
                matrixOccupanciesViewSource.Source = data;
 
            }
 
 
        }
 
        #endregion
 
        private void matrixDataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var selectedDate = (Matrix)e.AddedItems[0];
 
            var occupenciesCollection = selectedDate.Occupancies;
 
            var occupanciesViewSource = ((CollectionViewSource)(this.FindResource("matrixOccupanciesViewSource")));
 
            occupanciesViewSource.Source = occupenciesCollection;
        }

Par avance, merci.