新学VB,刚学到DATAGRIDVIEW,不想用控件来连接数据库,所以所有的连接都是代码的,可现在不知道怎么更新DATAGRIDVIEW里的数据到数据库存,从网上找了一个函数
Public Function CreateCommandAndUpdate(ByVal connectionString As String, ByVal queryString As String) As DataSet
Dim Fdt As DataSet = New DataSet
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand(queryString, connection)
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)
adapter.Fill(Fdt)
' Code to modify the data in the DataSet here.
' Without the OleDbCommandBuilder this line would fail.
builder.GetUpdateCommand()
adapter.Update(Fdt)
End Using
Return Fdt
End Function
运行到 builder.GetUpdateCommand()就说出错,原因是:对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand 的动态 SQL 生成。
感觉是queryString这个参数有问题,我传进来的是"select 姓名,职务 from 通讯录 where 类别='同学'"
不知道要怎么解决了,请版主指点一下,谢谢!