简体   繁体   English

关于SQL语句的语法,我错过了什么?

[英]What have I missed out with regards to the syntax of the SQL statement?

I am writing code to insert a username and password into a database called Users. 我正在编写代码以将用户名和密码插入到名为Users的数据库中。

When I try to run the code it says there is an error in the INSERT statement's syntax but I cannot for the life of me find it. 当我尝试运行代码时,它说INSERT语句的语法有错误,但我终生无法找到它。

I am running the SQL statement using another function called RunSQL that I can submit if need be but its worked fine with every other SQL statement I have run with it. 我正在使用另一个名为RunSQL的函数运行SQL语句,该函数可以在需要时提交,但它与我运行过的所有其他SQL语句都可以正常工作。

The Users table has the following columns with their data type User_ID - Auto Number (Primary Key) Username - Short Text Password - Short Text “用户”表具有其数据类型为以下几列的用户名-自动编号(主键)用户名-短文本密码-短文本

I have tried adding ' ' around the values I am going to insert into the table as well as removing the & and making it one continuous string. 我尝试在要插入表中的值周围添加'',并删除&并将其设置为一个连续的字符串。 I have tried adding / removing the ; 我试图添加/删除; but nothing has worked. 但没有任何效果。

Dim sql As String = "INSERT INTO Users (Username, Password) " &           
"VALUES (" & username_Textbox.Text & " , " & password_Textbox.Text & ");"
RunSQL(sql)
MessageBox.Show("User Added")

Private Sub RunSQL(ByVal sql As String)
    Dim conn As OleDbConnection = New 
    OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Paper_Gen_Database.accdb;")
    conn.Open()
    Dim cmd As New OleDbCommand(sql, conn)
    cmd.ExecuteNonQuery()
    System.Threading.Thread.Sleep(500)
End Sub

The code should take the values from the username and password textboxes and insert them into the Users table but so far it has only thrown back an SQL error. 该代码应从用户名和密码文本框中获取值,并将其插入到Users表中,但到目前为止,它仅抛出SQL错误。

This is what the SQL statement looks when with "bh106" being the Username and "ZLTga" being the Password 这是SQL语句的外观,其中“ bh106”为用户名,“ ZLTga”为密码

This is one way to use parameters. 这是使用参数的一种方法。 It is very important to use parameters because otherwise you risk SQL injection which can ruin your database. 使用参数非常重要,因为否则会冒着SQL注入的风险,这可能会破坏数据库。 It is actually much easier to write the SQL statement this way because you don't have to worry about if you have all your quotes in the string correctly. 用这种方式编写SQL语句实际上要容易得多,因为您不必担心字符串中的所有引号是否正确。

The Using...End Using blocks ensure that your database objects are closed and disposed even if there is an error. Using...End Using块可确保即使发生错误也可以关闭和处置数据库对象。 This is important because it releases any unmanaged resources being used. 这很重要,因为它会释放所有正在使用的非托管资源。

In a real application you would never save passwords as plain text but that is a subject for another day. 在真实的应用程序中,您永远不会将密码另存为纯文本,但这是另一天的主题。

Private Sub InsertUser()
    Dim sql As String = "INSERT INTO Users (Username, [Password]) VALUES (@username, @password);"
    Using conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Paper_Gen_Database.accdb;")
        Using cmd As New OleDbCommand(sql, conn)
            cmd.Parameters.Add("@username", OleDbType.VarChar).Value = username_Textbox.Text
            cmd.Parameters.Add("@password", OleDbType.VarChar).Value = password_Textbox.Text
            conn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
    MessageBox.Show("User Added")
End Sub

In Access the order that the parameters are added must match the order that they appear in the SQL statement. 在Access中,添加参数的顺序必须与它们在SQL语句中出现的顺序匹配。

Try this (its probably because of lack of quotes, and also because password is protected word): 尝试以下操作(它可能是因为缺少引号,也因为密码是受保护的单词):

Dim sql As String = "INSERT INTO Users (Username, [Password]) " &           
"VALUES ('" & username_Textbox.Text & "' , '" & password_Textbox.Text & "');"
RunSQL(sql)
MessageBox.Show("User Added")

Also be aware of sql injection problem. 还应注意sql注入问题。 If a user will put a quote inside a textbox, insert will still fail. 如果用户将引号放在文本框中,则插入仍将失败。

You should try converting your code into parametrized query, example: 您应该尝试将代码转换为参数化查询,例如:

https://docs.microsoft.com/pl-pl/dotnet/api/system.data.oledb.oledbcommand.parameters?view=netframework-4.7.2 https://docs.microsoft.com/pl-pl/dotnet/api/system.data.oledb.oledbcommand.parameters?view=netframework-4.7.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM