SQL 从 VB.NET 中的其他数据库源更新

SQL UPDATE From Other Database Sources In VB.NET

我想通过从列 (PRSOBNET) 中的另一个 table (IFGTS) 获取值,然后将该值与 [=21= 中 sql2 按钮的文本框中的值相加来进行更新] (GSDTS) 在列 (CIU) 中是否可以像我在“ciu”列中标记为黄色的屏幕截图结果那样做,如果我想将 sql1 按钮和 sql2 按钮合并为一个按钮命令怎么办。 for sql2 命令无效或未成功。请最佳解决方案。

谢谢 杰克

  Private Sub SQL1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SQL1.Click
        Try
            connectionString = cn
            con = New OleDbConnection(connectionString)
            con.Open()
            Dim sql As String = "UPDATE GSDTS SET CIUB = CIU,CIU = NULL,DPRB = DPR,DPR = NULL WHERE QTY > 0"
            cmd = New OleDbCommand(sql, con)
            cmd.ExecuteNonQuery()
            con.Close()
            Me.fillDataGridView1()
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub
    Private Sub SQL2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SQL2.Click
        Try
            connectionString = cn
            con = New OleDbConnection(connectionString)
            con.Open()
            Dim sql As String = "INSERT INTO GSDTS (CIU) SELECT t1.PRSOBNET FROM IFGTS t1 WHERE NOT EXISTS(SELECT ITM FROM GSDTS t2 WHERE t2.ITM = t1.ITM) AND GDN = 'A.04.01.002.001'"
            cmd = New OleDbCommand(sql, con)
            cmd.ExecuteNonQuery()
            con.Close()
            Me.fillDataGridView1()
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub

您可以从按钮处理程序中提取数据库代码,然后如果您想执行这两个操作,请调用两个提取的方法,如下所示:

Sub UpdateGsdts()
    Try
        Dim sql As String = "UPDATE GSDTS SET CIUB = CIU, CIU = NULL, DPRB = DPR, DPR = NULL WHERE QTY > 0"
        Using conn As New OleDbConnection(cn),
               cmd As New OleDbCommand(sql, conn)
            conn.Open()
            cmd.ExecuteNonQuery()
        End Using

    Catch myerror As OleDbException
        MessageBox.Show("Error: " & myerror.Message)
    End Try

End Sub

Sub InsertIntoGsdts()
    Try
        Dim sql As String = "INSERT INTO GSDTS (CIU) SELECT t1.PRSOBNET FROM IFGTS t1 WHERE NOT EXISTS(SELECT ITM FROM GSDTS t2 WHERE t2.ITM = t1.ITM) AND GDN = 'A.04.01.002.001'"
        Using conn As New OleDbConnection(cn),
               cmd As New OleDbCommand(sql, conn)
            conn.Open()
            cmd.ExecuteNonQuery()
        End Using

    Catch myerror As OleDbException
        MessageBox.Show("Error: " & myerror.Message)
    End Try

End Sub

Sub InsertIntoAndUpdateGsdts()
    InsertIntoGsdts()
    UpdateGsdts()
    fillDataGridView1()

End Sub

Private Sub SQL1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SQL1.Click
    UpdateGsdts()
    fillDataGridView1()

End Sub

Private Sub SQL2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SQL2.Click
    InsertIntoGsdts()
    fillDataGridView1()

End Sub

Using 语句确保数据库连接和命令使用的非托管资源在使用后被释放。