我是 vb.net 的新手,在使用数据集上的 For...Next 构造更新表中的字段时遇到了很大的挑战。下面的示例代码 - 谁能告诉我我错过了什么?我知道这个特定的例子可以用一个简单的 SQL 更新语句来完成,但是这个代码的实际用途需要逐步遍历数据集中的每条记录。下面的例子只是一个简单的例子,让我把程序搞定。请注意,这将毫无例外地运行,但不会更改表中的数据。
帮助!:)
Dim objConn As SqlConnection = New SqlConnection("Data Source=DALLAS\;Initial Catalog=Adaptive;Integrated Security=True")
Dim selectCMD As SqlCommand = New SqlCommand("SELECT * from dwbprocref", objConn)
selectCMD.CommandTimeout = 30
Dim custDA As SqlDataAdapter = New SqlDataAdapter
custDA.SelectCommand = selectCMD
Dim custCB As SqlCommandBuilder = New SqlCommandBuilder(custDA)
custCB.QuotePrefix = "["
custCB.QuoteSuffix = "]"
Dim dRow As DataRow
Dim dTable As DataTable
objConn.Open()
Dim custDS As DataSet = New DataSet
custDA.Fill(custDS, "dwbprocref")
For Each dRow In custDS.Tables(0).Rows
If dRow.Item("pr_format") = "MDV" Then
dRow.Item("pr_tester") = "X"
End If
custDS.AcceptChanges()
Next
custDA.Update(custDS, "dwbprocref")
objConn.Close()