我正在设计一个 Windows 用户表单。您可以在此处查看表单,问题是大多数文本框从未填充数据。我正在尝试使用表单顶部的按钮来“浏览”数据库并添加、修改和删除记录。
我似乎没有正确的 SQL 查询来从不同的表中获取所有数据,以便我DataAdapter
可以将其发送到表单。我正在寻求帮助来生成正确的查询并将数据传递给表单。您可以在此处查看数据库的构成。如果有人可以帮助我,那就太棒了。
表单的代码如下。
Imports System.Data.OleDb
Public Class DeviceEditorForm
Dim tabletDataAdapter As OleDbDataAdapter
Dim tabletDataSet As DataSet
Private Sub DeviceEditorForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.DeviceTableAdapter.Fill(Me.BIT_3444_DatabaseDataSet.Device)
Dim sqlStatement As String = "select * from Device"
Dim tabletConnectionStr As String = "provider=microsoft.ace.oledb.12.0;data source=" & Application.StartupPath & "\BIT 3444 Database.accdb"
tabletDataAdapter = New OleDbDataAdapter(sqlStatement, tabletConnectionStr)
tabletDataSet = New DataSet
tabletDataAdapter.Fill(tabletDataSet, "Device")
DeviceBindingSource.DataSource = tabletDataSet.Tables(0)
BindingNavigator1.BindingSource = DeviceBindingSource
Dim batterySizeBinding As New Binding("text", DeviceBindingSource, "Battery_Size")
BatterySizeTextBox.DataBindings.Add(batterySizeBinding)
Dim IDBinding As New Binding("text", DeviceBindingSource, "ID")
DeviceIdTextBox.DataBindings.Add(IDBinding)
Dim nameBinding As New Binding("text", DeviceBindingSource, "Device_Name")
DeviceNameTextBox.DataBindings.Add(nameBinding)
Dim manufacturerBinding As New Binding("text", DeviceBindingSource, "Manufacturer")
ManufacturerTextBox.DataBindings.Add(manufacturerBinding)
Dim modelYearBinding As New Binding("text", DeviceBindingSource, "Model_Year")
ModelYearTextBox.DataBindings.Add(modelYearBinding)
Dim osBinding As New Binding("text", DeviceBindingSource, "Operating_System")
OperatingSystemTextBox.DataBindings.Add(osBinding)
Dim osVersionBinding As New Binding("text", DeviceBindingSource, "Operating_System_Version")
OsVersionTextBox.DataBindings.Add(osVersionBinding)
Dim priceBinding As New Binding("text", DeviceBindingSource, "Price")
PriceTextBox.DataBindings.Add(priceBinding)
End Sub
Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
DeviceBindingSource.EndEdit()
Dim tabletCommandBuilder As New OleDbCommandBuilder(tabletDataAdapter)
tabletDataAdapter.InsertCommand = tabletCommandBuilder.GetInsertCommand()
tabletDataAdapter.DeleteCommand = tabletCommandBuilder.GetDeleteCommand()
tabletDataAdapter.UpdateCommand = tabletCommandBuilder.GetUpdateCommand()
Dim numOfRecords As Integer = tabletDataAdapter.Update(tabletDataSet, "Device")
MsgBox(numOfRecords & " records have been updated.")
End Sub
End Class