Really need help about this... 插入语句时出现语法错误
Private Sub Button2_Click(发送者作为对象,e作为EventArgs)处理Button2.Click
Try
connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\StJudeData2.accdb;persist security info = false"
conn.ConnectionString = connstring
Dim SqlQuery As String = "INSERT INTO StJudeData2(FirstName,MiddleInitial,LastName,Block#,Lot#,Telephone,HomeSticker#,Phase#) VALUES (@FirstName,@LastName,@Block#,@Lot#,@Telephone,@HomeSticker#,@Phase#)"
Dim SqlCommand As New OleDbCommand(SqlQuery, conn)
With SqlCommand
.CommandText = SqlQuery
.Connection = conn
conn.Open()
SqlCommand.Parameters.AddWithValue("@FirstName", TextBox1.Text)
SqlCommand.Parameters.AddWithValue("@LastName", TextBox2.Text)
SqlCommand.Parameters.AddWithValue("@MiddleIntial", TextBox3.Text)
SqlCommand.Parameters.AddWithValue("@Block#", TextBox4.Text)
SqlCommand.Parameters.AddWithValue("@Lot#", TextBox5.Text)
SqlCommand.Parameters.AddWithValue("@Telephone", TextBox6.Text)
SqlCommand.Parameters.AddWithValue("@HomeSticker#", TextBox7.Text)
SqlCommand.Parameters.AddWithValue("@Phase#", TextBox8.Text)
SqlCommand.ExecuteNonQuery()
End With
MsgBox("Records had been save!")
Catch ex As Exception
MsgBox(ex.ToString)
conn.Close()
End Try
1 回答
当您在列名中使用#这样的字符时,您应该记住将这些名称放在方括号中,因为它们会混淆Access
作为旁注,你真的需要它们吗?每次为此表编写查询时都会遇到此问题,因此,如果还不晚,请更改这些列名 .
如下所述,您的查询中存在另一个错误,MiddleInitial字段没有为其设置参数,您需要在@FirstName之后为MiddleInitial字段添加另一个参数(该位置对于Oletb不重要,因为OleDb不能识别参数他们的名字)