我正在尝试使用此代码将新订单添加到我的数据库中。我之前使用过相同的代码,但它现在不能正常工作。你能帮忙吗?
我正在使用Microsoft Visual Basic 2008快速版和Microsoft Access。
我有四张桌子。
CurrentRowNo = 0
' Purpose: Add new Account record and assign its default values
' Clear the Account table in the DataSet ready for a new record to be added
SandRDataSet.Tables("Stock").Clear()
'Add a new record to the studnet table
Dim driveletter As String = Application.StartupPath.Substring(0, 1)
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & driveletter & ":\raheem\Computing\Database\SandR.accdb")
cn.Open()
Dim daStock As New OleDbDataAdapter("SELECT * FROM Stock;", cn)
Dim sqlcmdbldStudent As New OleDb.OleDbCommandBuilder(daStock)
'Fill the database
daStock.Fill(SandRDataSet, "Stock")
Dim test As DataRow = SandRDataSet.Tables("Stock").NewRow
test("Product/Service number") = txtProductNo.Text
test("Product/ Service name") = txtProductName.Text
test("minimum level") = txtMin.Text
test("maximum level") = txtMax.Text
test("Quantity") = txtQuantity.Text
test("Price") = txtPrice.Text
SandRDataSet.Tables("Stock").Rows.Add(test)
sqlcmdbldStudent.GetInsertCommand()
daStock.InsertCommand = sqlcmdbldStudent.GetInsertCommand
daStock.Update(SandRDataSet.Stock)
DisplayAccount()
cn.Dispose()
cn.Close()
答案 0 :(得分:1)
如果你的表中确实有这些列名(空格和正斜杠),那么我建议你将QuotePrefix和QuoteSuffix设置为OleDbCommandBuilder
Dim sqlcmdbldStudent As New OleDb.OleDbCommandBuilder(daStock)
sqlcmdbldStudent.QuotePrefix = "["
sqlcmdbldStudent.QuoteSuffix = "]"
这将强制命令构建器创建的字段名称括在方括号中,并由数据库引擎正确解释