运行期间VB.net程序没有错误,但更新命令在ACCESS数据库中不起作用

时间:2013-11-18 14:28:10

标签: vb.net ms-access-2010 insert-update

运行期间VB.net程序没有错误,但更新命令在ACCESS数据库中不起作用。这是我的第一个问题,所以对于任何错误......提前抱歉。

 Dim con As New OleDbConnection
    Try

        Dim dslno As String
        Dim wno As String
        Dim rno As String
        Dim qnotaken As String
        Dim qnosold As String
        Dim damage As String
        Dim cinform As String   

        con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Shop Database.accdb")

        con.Open()

        If DataGridView1.Rows.Count > 0 Then
            Dim cmd As New OleDbCommand
            cmd.Connection = con
            cmd.CommandType = Data.CommandType.Text
            Dim values As String = ""

            For i As Integer = 0 To DataGridView1.Rows.Count - 2

                dslno = DataGridView1.Rows(i).Cells(0).Value
                wno = DataGridView1.Rows(i).Cells(2).Value
                rno = DataGridView1.Rows(i).Cells(3).Value
                qnotaken = DataGridView1.Rows(i).Cells(4).Value
                qnosold = DataGridView1.Rows(i).Cells(5).Value
                damage = DataGridView1.Rows(i).Cells(6).Value
                cinform = DataGridView1.Rows(i).Cells(7).Value

                If wno = "" Then
                    wno = 0
                End If
                If rno = "" Then
                    rno = 0
                End If
                If qnotaken = "" Then
                    qnotaken = 0
                End If
                If damage = "" Then
                    damage = 0
                End If
                If qnosold = "" Then
                    qnosold = 0
                End If
                If cinform = "" Then
                    cinform = "No"
                End If

                Dim strcommandText As String = "UPDATE  PRODUCT SET  ProductName='" + DataGridView1.Rows(i).Cells(1).Value & "', WPrice = " + wno + ",RPrice=" + rno + ",Damage=" + damage + ",TakenQuantity=" + qnotaken + ",SoldQuantity=" + qnosold + ",CONTACTINFORM='" + cinform + "'"


                values = strcommandText + (" Where SlNo=" + dslno).ToString
                MsgBox(values)
                cmd.CommandText = values
                cmd.ExecuteNonQuery()


            Next i



        End If


    Catch ex As Exception
        MsgBox("Enter all Details")
        MsgBox(ex.ToString)
        con.Close()
    Finally
        If con.State <> ConnectionState.Closed Then
            MsgBox("Sucess")
            con.Close()


        End If
    End Try  

我相信你可以帮助我,所以提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果你开始使用参数化查询,你将保存所以很多时间来调试代码中的丑陋小错误:

cmd.CommandText = _
        "UPDATE [PRODUCT] SET " & _
            "ProductName=?, " & _
            "WPrice=?, " & _
            "RPrice=?, " & _
            "Damage=?, " & _
            "TakenQuantity=?, " & _
            "SoldQuantity=?, " & _
            "CONTACTINFORM=? " & _
        "WHERE SlNo=?"
cmd.Parameters.AddWithValue("?", DataGridView1.Rows(i).Cells(1).Value)
cmd.Parameters.AddWithValue("?", wno)
cmd.Parameters.AddWithValue("?", rno)
cmd.Parameters.AddWithValue("?", damage)
cmd.Parameters.AddWithValue("?", qnotaken)
cmd.Parameters.AddWithValue("?", qnosold)
cmd.Parameters.AddWithValue("?", cinform)
cmd.Parameters.AddWithValue("?", dslno)
Dim rowsAffected As Integer = cmd.ExecuteNonQuery()