Dim ap As SqlClient.SqlDataAdapter'这个变量很重要
Private Function Save_Data() As Boolean '保存数据库的操作
Try
Me.DataGridView1.EndEdit()
' Me.BindingSource1 = Me.DataGridView1.DataSource
Me.BindingSource1.EndEdit()
Dim bd As New SqlClient.SqlCommandBuilder(ap)
ap.UpdateCommand = bd.GetUpdateCommand
ap.Update(Me.BindingSource1.DataSource)
Find(str_cmd)
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
首先将表读取到dataset
用datagridview绑定数据源
在
datagridview中直接修改字段的值后,用
sqldataadapter的update方法更新
dataset即可!