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
| Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class Nouveau_document
' Déclaration Objet Connexion
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() 'Attention au New
'String contenant la 'Requête SQL'
Private strSql As String
' Déclaration Objet DataTable
Private ObjetDataTable As DataTable
' Déclaration Objet DataRow (ligne)
Private ObjetDataRow As DataRow
'Numéro de la ligne en cours
Private RowNumber As Integer 'Numéro de l'enregistrement courant
'Paramêtres de connexion à la DB
Private strConn As String
'Pour recompiler les données modifiées avant de les remettre dans le "DataAdapter"
Private ObjetCommandBuilder As OleDbCommandBuilder
Private Sub Documents_ajout_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Initialisation de la chaîne de paramètres pour la connexion
strConn = "provider=microsoft.jet.oledb.4.0;data source=documents.mdb"
'Initialisation de la chaîne contenant l'instruction SQL
strSql = "SELECT documents.* FROM documents"
'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
'Avec l'aide de la propriété Fill du DataAdapter charger le DataSet
ObjetDataAdapter.Fill(ObjetDataSet, "documents")
'Mettre dans un Objet DataTable une table du DataSet
ObjetDataTable = ObjetDataSet.Tables("documents")
End Sub
Private Sub parcourir_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles parcourir.Click
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "C:\"
openFileDialog1.Filter = "Tous types de fichiers (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Insert code to read the stream here.
End If
Catch Ex As Exception
MessageBox.Show("Impossible de lire le fichier depuis le disque : " & Ex.Message)
Finally
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
Private Sub Valider_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Valider.Click
'Bouton ajouter
ObjetDataRow = ObjetDataSet.Tables("documents").NewRow()
ObjetDataRow("nom_document") = Me.TextBox_nom.Text()
ObjetDataRow("description_document") = Me.TextBox_description.Text
ObjetDataRow("mots_cle1") = Me.TextBox_motcle1.Text
ObjetDataRow("mots_cle2") = Me.TextBox_motcle2.Text
ObjetDataRow("mots_cle3") = Me.TextBox_motcle3.Text
ObjetDataRow("mots_cle4") = Me.TextBox_motcle4.Text
'Pour modifier les valeurs changées dans le DataAdapter
ObjetCommandBuilder = New OleDbCommandBuilder(ObjetDataAdapter)
'Mise à jour
Try
ObjetDataAdapter.Update(ObjetDataSet, "documents")
Catch
End Try
'On vide le DataSet et on le 'recharge' de nouveau.
ObjetDataSet.Clear()
ObjetDataAdapter.Fill(ObjetDataSet, "documents")
ObjetDataTable = ObjetDataSet.Tables("documents")
MsgBox("L'enregistrement a été réalisé avec succès")
Me.Close()
End Sub
End Class |
Partager