我有两个用于两个SqlCommand对象的更新sql语句,用DataSet对象将一个DataTable更新为一个源表。当我点击更新按钮时,只更新第一个,第二个SqlCommand对象不起作用。那是什么? DataTable一次不支持两个Command对象?我写如下:
Dim conxMain As New SqlConnection("Data Source=SERVER;Initial Catalog=DBTest;Persist Security Info=True;User ID=usr;Password=pwd")
Dim dadStockInfo As New SqlDataAdapter
Dim dsStockInfo As New DataSet
Dim transStockInfo As SqlTransaction = Nothing
Try
conxMain.Open()
transStockInfo = conxMain.BeginTransaction()
dadStockInfo.SelectCommand = New SqlCommand("SELECT * FROM Stock", conxMain, transStockInfo)
Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dadStockInfo)
dadStockInfo.FillSchema(dsStockInfo, SchemaType.Source, "Stock")
dadPurchaseInfo.Fill(dsPurchaseInfo, "Stock")
Dim cmdStockUpdateCmd As SqlCommand = Nothing
cmdStockUpdateCmd = New SqlCommand("UPDATE Stock SET StockCode= 'TestCode' WHERE StockID = 4", conxMain, transStockInfo)
dsStockInfo.Tables("Stock").Columns("StockID").ReadOnly = False
dadStockInfo.UpdateCommand = cmdStockUpdateCmd
//dadStockInfo.Update(dsStockInfo, "Stock") [I have already tried this but did not work]
Dim cmdStockUpdateCmd1 As SqlCommand = Nothing
cmdStockUpdateCmd1 = New SqlCommand("UPDATE Stock SET StockCode= 'Test-Code2' WHERE StockID = 1", conxMain, transPurchaseInfo)
dadStockInfo.UpdateCommand = cmdStockUpdateCmd1
dadStockInfo.Update(dsStockInfo, "Stock")
transStockInfo.Commit()
我不能像上面一样使用吗?请指出我!
你好!帮我! F1:)答案 0 :(得分:2)
您使用它的方式不需要数据适配器和数据集...
您只需执行2个更新语句。
你可以做到
cmdStockUpdateCmd1 = New SqlCommand("UPDATE Stock SET StockCode= 'Test-Code2' WHERE StockID = 1", conxMain, transPurchaseInfo)
cmdStockUpdateCmd1.ExecuteNonQuery();
数据集和数据适配器用于将数据传输到客户端 在客户端更改数据,然后将修改发送到服务器 您正在使用SQL直接在服务器上进行更改。