【问题标题】:SqlDataAdapter not update databaseSqlDataAdapter 不更新数据库
【发布时间】:2015-12-15 16:57:53
【问题描述】:

我是 VB 2015 的新手。我想了解数据库更新命令。我试着理解SqlDataAdapter。谁能给我建议?正如我下面的代码,它完全没有错误地运行,但是我的数据库表(WORKSHEET)没有更新。

Imports System.Data.SqlClient

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim command2 As String
    command2 = "Update WORKSHEET set cancel_flag = 'Y' WHERE CNumber LIKE @reversalNumber"
    Using con2 As New SqlConnection(WindowsApplication1.My.Settings.SaleCommDatabaseConnectionString)
        Using cmd2 As New SqlCommand(command2)
            Using oda2 As New SqlDataAdapter
                cmd2.Connection = con2
                con2.Open()
                cmd2.Parameters.Add("@reversalNumber", SqlDbType.VarChar, 10, "15332")
                oda2.UpdateCommand = New SqlCommand(command2, con2)

            End Using
        End Using
    End Using
    MsgBox("ggggg")
End Sub

End Class

【问题讨论】:

    标签: sql-server vb.net sqldataadapter


    【解决方案1】:

    SqlDataAdapter 类对于将更改从 DataSet 应用到数据库很有用。尝试使用修改后的数据填充 DataSet,然后使用适配器的更新方法 (oda2.Update(WORKSHEET)) 应用更改。

    编辑:确保使用 SqlDataAdapter 的Fill 方法用数据填充数据集。

    oda2.Fill(yourDataSet) 在此之前,您需要使用oda2.SelectCommand = YourCommand 选择正确的命令。

    【讨论】:

    • 我遇到了关于如何将数据集应用于 oda2 的问题。我尝试下面的代码,发生错误。似乎 tb2 是一个空对象。
    • 我应该更清楚。您必须自己实例化 DataSet,然后将更新应用到 DataSet,然后使用 DataSet 更新数据库中的表。这可能会有所帮助。 msdn.microsoft.com/en-us/library/…
    • 我更新了我之前的答案。确保您正在填充数据集。
    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多