DataTable无法使用DataAdapter(VB.NET)更新数据库

时间:2019-07-11 16:02:42

标签: sql-server vb.net

*注意:我删除了上一个问题“从DataTable和DataAdapter更新数据库”来代替这个问题。我已经将措辞和代码更新为我目前正在测试的内容。

我正在尝试使用WinForm中的信息更新数据库。使用手写的“正常” SQL更新命令(参数设置为文本框值)时,我没有任何问题,但是我试图清理并减少代码,我想将控件绑定到DataTable并使用一个DataAdapter的update命令来实现相同的目的。

我试图使设置参数和更新命令的各种组合正常工作,但是数据库没有从新的DataTable值中更新。我已经完成了每次更改的代码,可以看到DataTable正在获取新的文本框值,但是这些更新不会传递给数据库。 (这是在Fill_Date块运行并从数据库中选择所有新数据时看到的。)

我尝试过的事情:让绑定获取新值,而不是手动设置参数。使用命令构建器来构建更新命令,使用.UpdateCommand.ExecuteNonQuery(), command and of course a straight。Update(DataTable)命令。

下面是我正在使用的代码。我希望有人能告诉我我做错了什么/错过了什么,或者走的正确道路是什么。是否有“最佳实践”或更好的方法?

Public Class frmDATA
    Dim dt_Test As New DataTable
    Dim da_Test As New SqlDataAdapter
    Dim SQLcmd As SqlCommand

    Private Sub frmDemog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        BuildSQL()
        Fill_Data()
        BindControls()
    End Sub

    Private Sub frmDemog_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        If Not IsNothing(dt_Test) Then dt_Test.Dispose()
        If Not IsNothing(da_Test) Then da_Test.Dispose()
        If Not IsNothing(SQLcmd) Then SQLcmd.Dispose()
        Me.Dispose()
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Update_Me()
    End Sub

    Private Sub BindControls()
        txtLName.DataBindings.Add("Text", dt_Test, "Last_Name")
        txtFName.DataBindings.Add("Text", dt_Test, "First_Name")
        txtAKA.DataBindings.Add("Text", dt_Test, "AKA")
    End Sub

    Public Sub Update_Me(RefreshSearch As Boolean, RefreshView As Boolean)
        Try
            Dim testID As Integer = frmTest.dgvSearch.CurrentRow.Cells(0).Value
            da_Test.UpdateCommand.Parameters("@ID").Value = testID
            da_Test.Update(dt_Test)

            Fill_Data()

        Catch SqlExceptionErr As SqlException
            MsgBox(SqlExceptionErr.Message, vbCritical, "Error")
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Error")
        End Try
    End Sub

    Public Sub Fill_Data()
        Try
            dt_Test.Clear()
            da_Test.SelectCommand.Parameters("@ID").Value = testID
            da_Test.Fill(dt_Test)

        Catch SqlExceptionErr As SqlException
            MsgBox(SqlExceptionErr.Message, vbCritical, "Error")
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical, "Error")
        End Try
    End Sub

    Private Sub BuildSQL()
        '** Build Selection Query
        SQLcmd = New SqlCommand(String.Join(Environment.NewLine,
            "SELECT ",
                "data_Test.[Last_Name], ",
                "data_Test.[First_Name], ",
                "data_Test1.[Last_Name] + ', ' + data_Test1.[First_Name] as [AKA] ",
            "FROM [DB].data_Test ",
                "LEFT JOIN [DB].data_Test as data_Test1 ",
                "ON data_Test.[ID] = data_Test1.[AKA_Demog_ID] ",
            "WHERE data_Test.[ID]=@ID"
            ), Vars.sqlConnDB)

        SQLcmd.Parameters.Add("@ID", SqlDbType.Int)
        da_Test.SelectCommand = SQLcmd

        '** Build Update Query
        SQLcmd = New SqlCommand(String.Join(Environment.NewLine,
            "UPDATE [DB].data_Test SET ",
                "[Last_Name]  = @LName,",
                "[First_Name]  = @FName",
            "WHERE [ID] = @ID"
            ), Vars.sqlConnDB)

        With SQLcmd.Parameters
            .Add("@LName", SqlDbType.NVarChar, 255, "Last_Name") 'Required
            .Add("@FName", SqlDbType.NVarChar, 255, "First_Name") 'Required
            .Add("@ID", SqlDbType.Int, 0, "ID")
        End With

        da_Test.UpdateCommand = SQLcmd
    End Sub
End Class

0 个答案:

没有答案