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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: cette ligne de code charge les données dans la table 'NombreDataSet.TabNombre'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
Me.TabNombreTableAdapter.Fill(Me.NombreDataSet.TabNombre)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Déclaration Objet Connexion
Dim ObjetConnection As OleDbConnection
' Déclaration Objet Commande
Dim ObjetCommand As OleDbCommand
' Déclaration Objet DataAdapter
Dim ObjetDataAdapter As OleDbDataAdapter
' Déclaration Objet DataSet
Dim ObjetDataSet As New DataSet() 'Attention au New
'String contenant la 'Requête SQL'
Dim strSql As String
' Déclaration Objet DataTable
Dim ObjetDataTable As DataTable
' Déclaration Objet DataRow (ligne)
Dim ObjetDataRow As DataRow
'Numéro de la ligne en cours
Dim RowNumber As Integer 'Numéro de l'enregistrement courant
'Paramêtres de connexion à la DB
Dim strConn As String
'Pour recompiler les données modifiées avant de les remettre dans le
'"DataAdapter"
Dim ObjetCommandBuilder As OleDbCommandBuilder
'Initialisation de la chaîne de paramètres pour la connexion
strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= c:\Nombre.mdb;"
'Initialisation de la chaîne contenant l'instruction SQL
strSql = "SELECT TabNombre.* FROM TabNombre"
'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()
'Tentative de connexion à la BDD
Try
ObjetConnection.Open()
Catch ex As Exception
MessageBox.Show("Erreur lors de la connexion à la BDD ... Vérifiez votre connexion ou contactez votre administrateur..", "ERREUR", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
End Try
'Instancier un objet Commande
ObjetCommand = New OleDbCommand(strSql)
'Instancier un objet Adapter
ObjetDataAdapter = New OleDbDataAdapter(ObjetCommand)
'initialiser l'objet Command
ObjetCommand.Connection() = ObjetConnection
'Avec l'aide de la propriété Fill du DataAdapter charger le DataSet
ObjetDataAdapter.Fill(ObjetDataSet, "TabNombre")
'Mettre dans un Objet DataTable une table du DataSet
ObjetDataTable = ObjetDataSet.Tables("TabNombre")
' Extraire l'enregistrement courant
ObjetDataRow = ObjetDataSet.Tables("TabNombre").Rows(RowNumber)
'*********************************************************************
'*********************************************************************
'Modifier les valeurs des champs en récupérant le contenu des TextBox
ObjetDataRow("Resultat") = Me.ListBox2.Text * 3
'Pour modifier les valeurs changées dans le DataAdapter
ObjetCommandBuilder = New OleDbCommandBuilder(ObjetDataAdapter)
'Mise à jour
'On vide le DataSet et on le 'recharge' de nouveau.
ObjetDataSet.Clear()
ObjetDataAdapter.Fill(ObjetDataSet, "TabNombre")
ObjetDataTable = ObjetDataSet.Tables("TabNombre")
'fermer la connexion
'Objet connectée
ObjetConnection = Nothing
ObjetCommand = Nothing
ObjetDataAdapter = Nothing
'Objet déconnectée
ObjetDataSet = Nothing
ObjetDataTable = Nothing
ObjetDataRow = Nothing
MsgBox("Nombre modifiés !", MsgBoxStyle.Exclamation)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
End Class |
Partager