插入语句中的 C# 语法错误
本文关键字:语法 错误 语句 插入 | 更新日期: 2023-09-27 18:21:16
Dim strSQL As String
strSQL = "INSERT INTO TBLLOGIN (USERNAME,[PASSWORD],DISPLAYNAME,ROLEID,SECRETQUESTION,SECRETANSWER,ISACTIVE,STUDENTNUMBER, APPLICANTID,EMAILADDRESS) "
strSQL += "values( '" & Me.txtUName.Text & "' "
strSQL += ",'" & Me.txtPassword.Text & "' "
strSQL += ",'" & txtFirstName.Text & " " & txtMiddleName.Text & " " & txtLastName.Text & "' "
strSQL += ",'" & 6 & "' "
strSQL += ",'" & Me.ddlsecretquestion.SelectedValue & "' "
strSQL += ",'" & Me.txtSecretAnswer.Text & "' "
strSQL += ",'" & "NO" & "' "
strSQL += ",'" & studNumber & "' "
strSQL += "," & studNumber.Replace("SY15000", "") & " "
strSQL += ",'" & Me.txtUName.Text & "') "
Dim cmdQuery As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSQL, AccessConnectionString)
Dim xgen As Integer
cmdQuery.CommandType = Data.CommandType.Text
xgen = cmdQuery.ExecuteNonQuerystrong text
我不明白为什么我在插入语句中出现错误,我检查了保留词,据我所知,密码是那里唯一的保留词。 我的猜测是学生编号,因为它是一个字符串并且我正在更改值。 但我仍然不确定。 我通过使用会话(学生编号(尝试了几个解决方案,但仍然没有。
在此示例之后,使用参数化查询,如此方法所示:
Public Sub CreateMyOleDbCommand(connection As OleDbConnection, _
queryString As String, parameters() As OleDbParameter)
Dim command As New OleDbCommand(queryString, connection)
command.CommandText = _
"SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?"
command.Parameters.Add(parameters)
Dim j As Integer
For j = 0 To command.Parameters.Count - 1
command.Parameters.Add(parameters(j))
Next j
Dim message As String = ""
Dim i As Integer
For i = 0 To command.Parameters.Count - 1
message += command.Parameters(i).ToString() + ControlChars.Cr
Next i
Console.WriteLine(message)
End Sub
strSQL += "," & studNumber.Replace("SY15000", "") & " "
应该是
strSQL += ",'" & studNumber.Replace("SY15000", "") & "' "
您缺少引号,因为它是像STUDENTNUMBER这样的字符串。