Dim garcucon As New OleDb.OleDbConnection
Dim concmd As New OleDb.OleDbCommand
Dim da As OleDb.OleDbDataAdapter
Dim dv As DataView
Dim ds As DataSet
Datapath = "Provider=SQLOLEDB;Data Source=XX.XX.XX.XX;Initial Catalog="默认表"; user id=user1; password=password1;"
'查询按钮
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
garcucon.ConnectionString = Datapath
garcucon.Open()
concmd.Connection = garcucon
'查询结果输出
concmd.CommandText = "select * from add2 where 组别='GA'"
'MsgBox(concmd.CommandText)
da = New OleDb.OleDbDataAdapter()
da.SelectCommand = concmd
ds = New DataSet()
da.Fill(ds, "add2")
dv = New DataView(ds.Tables("add2"))
garcucon.Close()
Label1.Visible = True
Label1.Text = "满足条件的记录数有:" + dv.Count.ToString() + "条."
dv.Sort = "序列号 Asc"
DataGridView1.Visible = True
DataGridView1.DataSource = dv
Catch ex As Exception
End Try
End Sub
'在datagirdview1中修改 相关记录之后 button2 为更新数据库按钮,报错了..........
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
da.SelectCommand = concmd
DataGridView1.Update()
da.Update(ds, "add2")
ds.AcceptChanges()
End Sub
------解决方案--------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim oleBuilder As New OleDbCommandBuilder(da)
da.Update(ds, "add2")
end sub
------解决方案--------------------
Dim dbBlder = New OleDb.OleDbCommandBuilder(da)
Try
da.Update(ds.Tables("add2"))
MsgBox("数据库更新结束")
Catch
MsgBox("数据库更新失败")
End Try