'Vorbereitungsarbeiten: ' 'Menü "Daten" - "Neue Datenquelle hinzufügen" 'Table Adapter auf Formular ziehen 'Data Set auf Formular ziehen
Public Class frmDatabase Private Sub subMountDatabase() Try 'Open Table Adapter (filename is predefined in the connection string - Resources) tblXYZTableAdapter.Connection.Open() Catch ex As Exception MsgBox("The connection to the Database 'C:\Database.accdb' failed." & ControlChars.CrLf & "Please make sure that the database exists and the file is not corrupted.", MsgBoxStyle.Critical, "Database error") End Try End Sub Private Sub subDismountDatabase() Try 'Close Table Adapter tblXYZTableAdapter.Connection.Close() Catch ex As Exception MsgBox("The connection could not be closed", MsgBoxStyle.Critical, "Database error") End Try End Sub Private Sub subSearchDatset Dim rowFound As DataRow() Dim strSearchTerm As String Dim strFieldContent As String 'Fill DataSet with a specific Table from the Database tblXYZTableAdapter.Fill(Database_XYZDataSet.tblTABLE) 'Find a certain Row in the DataSet and write it to the rowFound variable 'FIELD1 = name of the data field 'strSearchTerm = the thing you are searching for rowFound = Database_XYZDataSet.tblTABLE.Select("FIELD1 = '" & strSearchTerm & "'") 'Write the content of a certain field in the row we have just found to the strFieldContent variable strFieldContent = rowFound(0).Item("FIELD1").ToString End Sub Private Sub subAddDataset Dim rowNew As DataRow 'Schema definieren rowNew = Database_XYZDataSet.tblTABLE.NewRow rowNew.Item("FIELD1") = "string1" rowNew.Item("FIELD2") = "string2" Database_XYZDataSet.tblTABLE.Rows.Add(rowNew) tblXYZTableAdapter.Update(Database_XYZDataSet) End Sub End Class |