0

这是我要运行的代码。它会正常运行,但不会更新我的数据库。

当它没有参数化时它会起作用,但是当我在其中添加参数时它开始起作用。这是有问题的代码。

Public Sub updateItem()

    Dim sqlConnection1 As New OleDb.OleDbConnection(dbProvider + dbSource)
    Dim cmd As New OleDb.OleDbCommand

    cmd.CommandText = "Update Inventory set PartNumber='@PartNumber', Brand='@Brand', PartDescription='@PartDescription', PartCost=@PartCost, InventoryOnHand=@InventoryOnHand, PartSupplier='@PartSupplier' where PartNumber = '@PartNumMatch' and Brand = '@PartManMatch';"
    cmd.Parameters.AddWithValue("@PartNumber", partNumberText.Text().ToUpper())
    cmd.Parameters.AddWithValue("@Brand", ManufacturerText.Text())
    cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text())
    cmd.Parameters.AddWithValue("@PartCost", Convert.ToDouble(partCostText.Text()))
    cmd.Parameters.AddWithValue("@InventoryOnHand", Convert.ToInt32(quantityText.Text()))
    cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text())
    cmd.Parameters.AddWithValue("@PartNumMatch", partNumberText.Text().ToUpper().Trim())
    cmd.Parameters.AddWithValue("@PartManMatch", ManufacturerText.Text().ToUpper().Trim())


    cmd.CommandType = CommandType.Text
    cmd.Connection = sqlConnection1

    Try

        sqlConnection1.Open()

        cmd.ExecuteNonQuery()

        sqlConnection1.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
        sqlConnection1.Close()
    End Try

    'SQl statement to try to update the selected row's data matched against the database.

    'update listview here.
End Sub

我几乎可以肯定语法是正确的,因为我的插入有效。这是我插入的代码。

Private Sub addItem()
    'SQL statement here to add the item into the database, if successful, move the information entered to listview.

    Dim sqlConnection1 As New OleDb.OleDbConnection(dbProvider + dbSource)
    Dim cmd As New OleDb.OleDbCommand
    'Dim reader As SqlDataReader

    cmd.CommandText = "Insert into Inventory ([PartNumber], [Brand], [PartDescription], [PartCost], [InventoryOnHand], [PartSupplier]) values (@PartNumber, @Brand, @PartDescription, @PartCost, @InventoryOnHand, @PartSupplier);"
    cmd.Parameters.AddWithValue("@PartNumber", partNumberText.Text().ToUpper().Trim())
    cmd.Parameters.AddWithValue("@Brand", ManufacturerText.Text().ToUpper().Trim())
    cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text().Trim())
    cmd.Parameters.AddWithValue("@PartCost", partCostText.Text())
    cmd.Parameters.AddWithValue("@InventoryOnHand", quantityText.Text())
    cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text().Trim())
    cmd.CommandType = CommandType.Text
    cmd.Connection = sqlConnection1
    Dim found As Boolean = False

    Try
        sqlConnection1.Open()

        cmd.ExecuteNonQuery()

        MessageBox.Show(cmd.CommandText)


        sqlConnection1.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
        sqlConnection1.Close()
    End Try

End Sub

我知道 where 子句是正确的,我对值进行了硬编码,我还将值与消息框进行比较,并将它们直接与数据库中的信息进行比较。

提前感谢任何和所有的意见,我希望我们能弄清楚。

4

2 回答 2

2

参数占位符不应用单引号括起来

cmd.CommandText = "Update Inventory set PartNumber=@PartNumber, Brand=@Brand, " + 
                  "PartDescription=@PartDescription, PartCost=@PartCost, " + 
                  "InventoryOnHand=@InventoryOnHand, PartSupplier=@PartSupplier " +  
                  "where PartNumber = @PartNumMatch and Brand = @PartManMatch;"

您不需要这样做,它只会混淆尝试将参数占位符替换为实际值的代码。它们将被视为文字字符串

于 2013-04-15T22:20:05.817 回答
0

尝试这个,

cmd.CommandText = "Update Inventory set PartNumber=@PartNumber, Brand=@Brand, " + 
                  "PartDescription=@PartDescription, PartCost=@PartCost, " + 
                  "InventoryOnHand=@InventoryOnHand, PartSupplier=@PartSupplier " +  
                  "where PartNumber = @PartNumMatch and Brand = @PartManMatch;"

cmd.Parameters.AddWithValue("@PartDescription", partDescriptionText.Text())
cmd.Parameters.AddWithValue("@PartCost", Convert.ToDouble(partCostText.Text()))
cmd.Parameters.AddWithValue("@InventoryOnHand", Convert.ToInt32(quantityText.Text()))
cmd.Parameters.AddWithValue("@PartSupplier", partSupplierText.Text())
cmd.Parameters.AddWithValue("@PartNumMatch", partNumberText.Text().ToUpper().Trim())
cmd.Parameters.AddWithValue("@PartManMatch", ManufacturerText.Text().ToUpper().Trim())
于 2013-10-11T06:26:13.407 回答