我在INSERT INTO语句中遇到语法错误,可能是什么原因?
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Bro_Proj.accdb"
Dim sql As String = "INSERT INTO Order(`Stud_ID`, `BookName`, `Quantity`, `sale`, `DateOfOrder`, `TimeOfOrder`) VALUES (' " & TextBox1.Text & " ' , ' " & ComboBox3.Text & " ', " & NumericUpDown1.Value & ", ' " & TextBox10.Text & " ', # " & TextBox8.Text & " #, # " & TextBox9.Text & " #);"
conn.Open()
Dim sqlc As New System.Data.OleDb.OleDbCommand(sql, conn)
sqlc.ExecuteNonQuery()
conn.Close()
答案 0 :(得分:1)
我认为这是因为当您附上日期值时,空格字符存在 试着省略它:
Dim sql As String = "INSERT INTO Order(`Stud_ID`, `BookName`, `Quantity`, `sale`, `DateOfOrder`, `TimeOfOrder`)
VALUES (' " & TextBox1.Text & " ' ,
' " & ComboBox3.Text & " ',
" & NumericUpDown1.Value & ",
' " & TextBox10.Text & " ',
#" & TextBox8.Text & "#,
#" & TextBox9.Text & "#);"
答案 1 :(得分:1)
我怀疑这个问题要么与#符号和日期之间的空格有关,要么“NumericUpDown1.Value”不是一个字符串而不被告知这样做。
使用这种插入物很容易出错。我通常使用类似下面的东西。 (对不起,如果它不完美。我对c#更熟悉,这是我尝试翻译的。)
Dim sqlc As New System.Data.OleDb.OleDbCommand()
sqlc.CommandText = "INSERT INTO Order(Stud_ID, BookName, Quantity, sale, DateOfOrder, TimeOfOrder) VALUES (@Stud_ID, @BookName, @Quantity, @sale, @DateOfOrder, @TimeOfOrder)
sqlc.Parameters.AddWithValue("@Stud_ID", TextBox1.Text)
sqlc.Parameters.AddWithValue("@BookName", ComboBox3.Text)
sqlc.Parameters.AddWithValue("@Quantity", NumericUpDown1.Value)
sqlc.Parameters.AddWithValue("@sale", TextBox10.Text)
sqlc.Parameters.AddWithValue("@DateOfOrder", "#" & TextBox8.Text & "#")
sqlc.Parameters.AddWithValue("@TimeOfOrder", "#" & TextBox9.Text & "#")
try {
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Bro_Proj.accdb"
sqlc.Connection = conn;
conn.Open();
sqlc.ExecuteNonQuery();
conn.close();
}
catch (Exception ex) {
textBox1.Text = ex.ToString();
}