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
|
Imports System.Data.OleDb
Public Class FormAwesome
Private firstSql As String
Private secSQL As String
Private myConnection As OleDbConnection
'declare ici une variable commande en portee class (ton form)
Private myCmd As OleDbCommand
Private adapt As OleDb.OleDbDataAdapter
'declare ici le builder en porte class
Private CmdBuild As OleDb.OleDbCommandBuilder
Private ds As DataSet
'un seul manager suffit pour tous les controles lies de la forme
Private myManager As BindingManagerBase
Private Sub FormAwesome_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click
myConnection = New OleDb.OleDbConnection
myConnection.ConnectionString = My.Settings.MEDECINECnStr
'init adapter
adapt = New OleDb.OleDbDataAdapter
'init commande avec connection
myCmd = New OleDbCommand
myCmd.Connection = myConnection
'init ds
ds = New DataSet
'fill "patient"
firstSql = "SELECT * FROM Patient"
myCmd.CommandText = firstSql
adapt.SelectCommand = myCmd
adapt.Fill(ds, "patient")
'fill "docteur"
secSQL = "SELECT * FROM Docteur"
myCmd.CommandText = secSQL
adapt.Fill(ds, "docteur")
myManager = Me.BindingContext(ds, "patient")
txt_nom.DataBindings.Add(New Binding("text", ds, "patient.nom_patient"))
txt_id_patient.DataBindings.Add(New Binding("text", ds, "patient.id_patient"))
Me.DataGridViewPatient.DataSource = Me.ds
Me.DataGridViewPatient.DataMember = Me.ds.Tables("patient").TableName
txt_doc_nom.DataBindings.Add(New Binding("text", ds, "docteur.nom_docteur"))
txt_id_docteur.DataBindings.Add(New Binding("text", ds, "docteur.id_docteur"))
Me.DataGridViewDocteur.DataSource = Me.ds
Me.DataGridViewDocteur.DataMember = Me.ds.Tables("docteur").TableName
End Sub
'Pour le bouton ajouter
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' Abus dans le nom des variables :
'ta variable ligne contient n'importe n'importe quoi
Dim LaLigne As DataRow
'ajout ligne à table "patient"
'ta variable LaLigne pointe maintenant pointe vers "patient"
LaLigne = ds.Tables("patient").NewRow()
LaLigne("nom_patient") = Trim(txt_nom.Text)
LaLigne("id_patient") = Trim(txt_id_patient.Text)
ds.Tables("patient").Rows.Add(LaLigne)
'init builder
CmdBuild = New OleDbCommandBuilder(adapt)
'builder doit pointer sur table "patient" en reinitialisant commandText
myCmd.CommandText = firstSql
CmdBuild.GetInsertCommand() 'utilise ca (& update,ou delete suivant le cas)
adapt.Update(ds, "patient")
'ta variable LaLigne pointe maintenant pointe vers "docteur"
LaLigne = ds.Tables("docteur").NewRow()
LaLigne("nom_docteur") = Trim(txt_doc_nom.Text)
LaLigne("id_docteur") = Trim(txt_id_docteur.Text)
ds.Tables("docteur").Rows.Add(LaLigne)
'builder pointe sur "docteur" en reinitialisant commandText
myCmd.CommandText = secSQL
CmdBuild.GetInsertCommand() 'utilise ca & update,ou delete suivant le cas
adapt.Update(ds, "docteur")
'code inutile.pourquoi?
'adapt.Fill(ds, "patient")
'adapt.Fill(ds, "docteur")
End Sub
End Class |
Partager