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 101 102 103 104 105 106 107 108
|
Public Class Form1
' Déclaration Objet Connection
Private ObjetConnection As OleDbConnection
' Déclaration Objet Commande
Private ObjetCommand As OleDbCommand
' Déclaration Objet DataAdapter
Private ObjetDataAdapter As OleDbDataAdapter
' Déclaration Objet DataSet
Private ObjetDataSet As New DataSet
' Déclaration Objet DataTable
Private ObjetDataTable As New DataTable
'String contenant la 'Requête SQL'
Private strSql As String
'Paramêtres de connexion à la DB
Private strConn As String
' Déclaration d'un OleDbCommandBuilder
Private ObjetCB As OleDbCommandBuilder
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
'Le bouton ButtonAfficheGrid remplie le DataGrid avec DataBase1.accdb table 'STOCK_ASD'
Private Sub ButtonAfficheGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
_Handles(ButtonAfficheGrid.Click)
'Initialisation de la chaîne de paramètres pour la connexion
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source= C:\Users\alexandre\Documents\Gesto\WindowsApplication1\bin;"
'Initialisation de la chaîne contenant l'instruction SQL
strSql = "SELECT STOCK_ASD.* FROM STOCK_ASD"
'Instanciation d'un Objet Connexion
ObjetConnection = New OleDbConnection
'Donner à la propriété ConnectionString les paramètres de connexion
ObjetConnection.ConnectionString = strConn
'Ouvrir la connexion
ObjetConnection.Open()
'Instancier un objet Commande
ObjetCommand = New OleDbCommand(strSql)
'Instancier un objet Adapter
ObjetDataAdapter = New OleDbDataAdapter(ObjetCommand)
'initialiser l'objet Command
ObjetCommand.Connection() = ObjetConnection
'initialiser l'objet OleCBComandBuilder (sinon pas d'update)
ObjetCB = New OleDbCommandBuilder(ObjetDataAdapter)
'Avec l'aide de la propriété Fill du DataAdapter charger le DataSet
ObjetDataAdapter.Fill(ObjetDataSet, "STOCK_ASD")
'Créer une datatable à partir du dataset
ObjetDataTable = ObjetDataSet.Tables("STOCK_ASD")
'Mettre dans le DataGrid une table DataTable
DataGrid1.DataSource = ObjetDataTable
End Sub
'Le bouton ButtonMiseA jour met à jour DataBase1.accdb si on a fait une modification dans le DataGrid.
Private Sub ButtonMiseAJour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
_Handles(ButtonMiseAJour.Click)
'Mettre à jour
ObjetDataAdapter.Update(ObjetDataSet, "STOCK_ASD")
End Sub |
Partager