VB.NET 数据集更新

发布于 2024-08-07 05:30:32 字数 1108 浏览 9 评论 0原文

我是 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()

I'm new to vb.net and having quite a challenge with updating a field in a table using the For...Next construct on a data set. Sample code below - can anyone tell me what I'm missing? I know this particular example could be accomplished with a simple SQL update statement, but the actual use for what this code will become requires stepping through each record in the dataset. The example below is just a simple one to let me get the procedure down. Note that this runs without an exception, but just doesn't change the data in the table.

Help! :)


    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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女净妖师 2024-08-14 05:30:32

您正在调用 AcceptChanges。这将所有行标记为未修改,因此它们永远不会在数据库中更新。删除这个电话,你应该就好了。

You're calling AcceptChanges. This marks all of the rows as being unmodified, so they are never updated in the database. Remove this call and you should be good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文