这是我尝试运行的代码。它将运行没有错误,但它不会更新我的数据库。
当它没有参数化时会起作用,但当我在其中添加参数时,它会开始起作用。这是有问题的代码。
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子句是正确的,我已经对值进行了硬编码,并且我还将值与消息框进行了比较,并将它们直接与数据库中的信息进行比较。
提前感谢任何和所有意见,我希望我们能够理解它。
答案 0 :(得分:2)
参数占位符不应用单引号括起来
cmd.CommandText = "Update Inventory set PartNumber=@PartNumber, Brand=@Brand, " +
"PartDescription=@PartDescription, PartCost=@PartCost, " +
"InventoryOnHand=@InventoryOnHand, PartSupplier=@PartSupplier " +
"where PartNumber = @PartNumMatch and Brand = @PartManMatch;"
您不需要这样做,它只会将尝试替换参数占位符的代码与实际值混淆。它们将被视为文字字符串
答案 1 :(得分: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())