[英]SQL insert into error VB.NET
running a vb application with the following code. 使用以下代码运行vb应用程序。 I keep getting an error on my 'INSERT INTO' sql query, can anyone see what im doing wrong?
我在“ INSERT INTO” SQL查询中不断收到错误,有人可以看到我在做什么错吗? This is the error - Syntax error in INSERT INTO statement.
这是错误-INSERT INTO语句中的语法错误。
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Dave\Documents\joblist.mdb;"
connection = New OleDb.OleDbConnection(connetionString)
Sql = "INSERT INTO jobList (StaffID, staffName, staffLastName, note, fault, section, techID, jobcomplete) VALUES ('" & staffid & "','" & staffFN & "','" & staffLN & "','" & staffNotes & "','" & staffFault & "', '" & staffSection & "', '" & techId & "','" & ava & "')"
connection.Open()
oledbAdapter.UpdateCommand = connection.CreateCommand
oledbAdapter.UpdateCommand.CommandText = Sql
oledbAdapter.UpdateCommand.ExecuteNonQuery()
connection.Close()
Me.JobListTableAdapter.Fill(Me.JoblistDataSet2.jobList)
I assume you should use the InsertCommand
instead of the UpdateCommand
property since you are inserting. 我假设您正在插入,因此应该使用
InsertCommand
而不是UpdateCommand
属性。
oledbAdapter.InsertCommand.CommandText = "INSERT INTO jobList (StaffID, staffName, ....
Note that you're open for SQL-Injection and should use Parameters
instead. 请注意,您可以使用SQL-Injection,而应改用
Parameters
。 You should also use Using
statement to ensure that the connection gets closed even on error. 您还应该使用
Using
语句来确保即使出错也可以关闭连接。
Note
and Section
are reserved words in Jet SQL You need to encapsulate them with square brackets Note
和Section
是Jet SQL中的保留字,您需要使用方括号将它们封装起来
Sql = "INSERT INTO jobList (StaffID, staffName, staffLastName, [note], fault, " +
"[section], techID, jobcomplete) VALUES (......)"
This is the source of you syntax error, but ..... 这是您语法错误的来源,但是.....
aside from that, you have many problems here: 除此之外,这里还有很多问题:
staffID
and techID
seems to be numeric fields in the database, but you put their values inside single quotes like strings. staffID
和techID
似乎是数据库中的数字字段,但是您将它们的值放在单引号内,例如字符串。 If they are numerics you will get another possible error there.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.