VB.Net更新不会更新我的数据库
问题描述:
这是我试图运行的代码。它将运行没有错误,但它不会更新我的数据库。VB.Net更新不会更新我的数据库
它会工作时,它不参数化,但当我添加参数开始行动。这是有问题的代码。
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', [email protected], [email protected], 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子句是正确的,我已经硬编码的价值的,我也推值的进行比较,以消息框的,并直接把它们比作在数据库中的信息。
在此先感谢您的任何意见,我希望我们能把它弄清楚。
答
参数占位符不应该用单引号括
cmd.CommandText = "Update Inventory set [email protected], [email protected], " +
"[email protected], [email protected], " +
"[email protected], [email protected] " +
"where PartNumber = @PartNumMatch and Brand = @PartManMatch;"
你并不需要做的,它只是混淆试图与实际值来代替参数占位符的代码。他们将被视为文字字符串
+0
工作,谢谢! 令人惊讶的是,这样的小事可能会让你失望。我认为他们需要在那里,而这一假设浪费了大约3个小时的时间。 >。 Josiah
答
试试这个,
cmd.CommandText = "Update Inventory set [email protected], [email protected], " +
"[email protected], [email protected], " +
"[email protected], [email protected] " +
"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())
在'UPDATE'语句中移除周围的参数报价:'更新清单设置部分号码=“@ PARTNUMBER” ...'应该是'更新库存集PartNumber = @ PartNumber ...' –