我几乎可以用下面的 SqlAdapter 做事
Private SqlDA As New SqlDataAdapter("Select * from tblContacts", SqlConn)
但是,如果我在下面使用,那么在我从 SQL 检索数据以修改然后使用 DataTable 更新之后,我会因为以下使用而被抛出。顺便说一句,第一个字段上有主键,即身份意味着您知道自动增加。下面不包含该字段的标题。(ContactID)
Private SqlDA As New SqlDataAdapter("Select FirstName, LastName, Email, PhoneNumber, Tarih from tblContacts", SqlConn)
这是整个代码:
Dim TheDataTable As New DataTable
Private SqlConn As New SqlConnection("Data Source=PC-N39\sqlexpress;Initial Catalog=ADO_PRACTICE;Persist Security Info=True;User ID=sa;Password=sas")
Private SqlDA As New SqlDataAdapter("Select * from tblContacts", SqlConn)
Private SqlDataSet As New DataSet
Private SqlCmdBuilder As New SqlCommandBuilder(SqlDA)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
SqlDA.Fill(TheDataTable)
DataGridView1.DataSource = TheDataTable
InitializeDataGridView()
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim x As Integer
x = SqlDA.Update(TheDataTable)
MessageBox.Show(CStr(x) & " record(s) updated")
End Sub