insert语句中的语法错误 [英] Syntax error in insert statement
问题描述
大家好,
我收到以下语句的语法错误。但是我找不到它,请检查并告诉我你是否能找到它。声明是;
使用sqlquery作为新的OleDb.OleDbCommand(INSERT INTO SFA Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(? ,?,?,?,?,?,?),sqlconn)
我尝试过:
我的整个代码是;
Hi guys,
I am getting syntax error for below statement. But I can't find it, Please check and let me know if you can find it. Statement is;
Using sqlquery As New OleDb.OleDbCommand("INSERT INTO SFA Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(?,?,?,?,?,?,?)", sqlconn)
What I have tried:
My Entire Code is;
private Sub cmdStockAdd_Click(sender As Object, e As EventArgs) Handles cmdStockAdd.Click
Dim res As String
res = MsgBox("Do you want to Add this record", vbYesNo, "SFA-Inventory Add")
If res = vbYes Then
Try
Using sqlconn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Data\SFA\My Project\SFA Inventory\SFA Inventory\SFA Inventory.accdb")
Using sqlquery As New OleDb.OleDbCommand("INSERT INTO SFA Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(?,?,?,?,?,?,?)", sqlconn)
sqlquery.Parameters.AddWithValue("@IMEI No", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@Product Name", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@Invoice No", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@Invoice Date", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@Mobile No", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@App Version", TextBox5.Text)
sqlconn.Open()
sqlquery.ExecuteNonQuery()
MsgBox("Records inserted successfully", vbOKOnly, "Records inserted")
TextBox2.Text = ""
TextBox1.Text = ""
ComboBox1.Text = ""
TextBox3.Text = ""
DateTimePicker1.Value = DateTime.Now
TextBox4.Text = ""
TextBox5.Text = ""
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
Show()
End If
End Sub
推荐答案
在表名和列名中放置空格是个坏主意。如果你这样做,你必须将名称括在方括号中,以确保名称与空格一起考虑:
It is a bad idea to put spaces in table and column names. If you do that, you have to enclose the names in square brackets to make sure the name is considered with the spaces:
INSERT INTO [SFA Inventory] (IMEINo, ProductName, Supplier, InvoiceNo, InvoiceDate, MobileNo, AppVersion) VALUES (@IMEINo, @ProductName, @Supplier, @InvoiceNo, @InvoiceDate, @MobileNo, @AppVersion)
另外,删除命名参数中的所有空格:
Also, remove all of the spaces in your named parameters:
sqlquery.Parameters.AddWithValue("@IMEINo", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@ProductName", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@InvoiceNo", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@InvoiceDate", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@MobileNo", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@AppVersion", TextBox5.Text)
你还会制造另一个严重的薄雾AKE。切勿直接在查询中使用文本框的值。始终验证输入的数据并使用参数中的验证数据。
You're also make another grievous mistake. NEVER directly use the values of textboxes in your queries. ALWAYS validate the data that's entered and use the validated data in the parameters.
尝试
try
Using sqlquery As New OleDb.OleDbCommand("INSERT INTO SFA Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(@IMEINo,@ProductName,@Supplier,@InvoiceNo,@InvoiceDate,@MobileNo,@AppVersion)", sqlconn)
sqlquery.Parameters.AddWithValue("@IMEINo", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@ProductName", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@InvoiceNo", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@InvoiceDate", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@MobileNo", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@AppVersion", TextBox5.Text)
你好,>
Hello ,
Inventory(IMEINo,ProductName,Supplier,InvoiceNo,InvoiceDate,MobileNo,AppVersion)VALUES(?,?,?,?,?,?,?)",
有空间
和这里有
with out space
and here with
sqlquery.Parameters.AddWithValue("@IMEI No", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@Product Name", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@Invoice No", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@Invoice Date", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@Mobile No", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@App Version", TextBox5.Text)
更改为
Change it to
sqlquery.Parameters.AddWithValue("@IMEINo", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@ProductName", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Supplier", ComboBox1.Text)
sqlquery.Parameters.AddWithValue("@InvoiceNo", TextBox3.Text)
sqlquery.Parameters.AddWithValue("@InvoiceDate", DateTimePicker1.Value.ToShortDateString())
sqlquery.Parameters.AddWithValue("@MobileNo", TextBox4.Text)
sqlquery.Parameters.AddWithValue("@App Version", TextBox5.Text)
这篇关于insert语句中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!