简体   繁体   English

如何修复 vbnet 中 INSERT INTO 语句中的语法错误?

[英]How do fix Syntax error in INSERT INTO statement in vbnet?

I am trying to update my database and ran into this error.我正在尝试更新我的数据库并遇到此错误。 I am trying to update my database by adding new rows and run into this error.我正在尝试通过添加新行来更新我的数据库并遇到此错误。

    Dim dsNewRow As DataRow
    Dim ds As New DataSet

    Dim con As New OleDbConnection

    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\POS DATABASE.mdb"
    con.Open()

    Dim sql As String = "SELECT * FROM manager_login_data"

    Dim da As New OleDbDataAdapter(sql, con)

    Dim cb As New OleDb.OleDbCommandBuilder(da)


    da.Fill(ds, "manager_login_data")

    dsNewRow = ds.Tables("manager_login_data").NewRow()

    dsNewRow.Item("username") = TextBox1.Text
    dsNewRow.Item("password") = TextBox2.Text



    ds.Tables("manager_login_data").Rows.Add(dsNewRow)


    da.Update(ds, "manager_login_data")

I want to know what I need to do in order to fix this and get my program running.我想知道我需要做什么才能解决这个问题并使我的程序运行。 The error takes place at the da.Update line.错误发生在 da.Update 行。

EDIT: I found out that the sql command I am feeding the da has to be changed.编辑:我发现必须更改我提供给 da 的 sql 命令。

I just need to know into what.我只需要知道什么。

EDIT 2:编辑2:

我的访问数据库

You don't necessarily need to change your query.您不一定需要更改查询。 What is almost certainly happening is that one of your column names is a reserved word or contains spaces or other special characters.几乎可以肯定发生的是,您的列名之一是保留字或包含空格或其他特殊字符。 There are a number of ways to address this issue.有多种方法可以解决这个问题。

The first and best option is to change your column name(s).第一个也是最好的选择是更改您的列名称。 You should never use spaces or special characters in column names and you should avoid reserved words.永远不要在列名中使用空格或特殊字符,并且应该避免使用保留字。 It appears that this is login data in Access, which makes it likely that one of your columns is named Password .这似乎是 Access 中的登录数据,这使得您的列之一很可能被命名为Password That is indeed a reserved word in Jet SQL.这确实是 Jet SQL 中的保留字。 If you were doing things properly then you would not be storing passwords in clear text to begin with but, rather, hashing them instead.如果您正确地做事,那么您一开始就不会以明文形式存储密码,而是将它们散列。 In that case, the column could be named PasswordHash and the issue goes away.在这种情况下,该列可以命名为PasswordHash ,问题就会消失。 If you can't change the column name to something more suitable, you need to try a different option.如果您无法将列名更改为更合适的名称,则需要尝试其他选项。

Another option is to not use wildcards in your query.另一种选择是不在查询中使用通配符。 A command builder will simply use column names as they are if you do but, if you specify the columns explicitly, the command builder will use the same names you do.如果您这样做,命令构建器将简单地使用列名称,但是,如果您明确指定列,命令构建器将使用与您所做的相同的名称。 In that case, you would need to escape any problem column names in your query and then the command builder will do the same, eg在这种情况下,您需要对查询中的任何问题列名称进行转义,然后命令生成器将执行相同的操作,例如

Dim sql As String = "SELECT UserName, [Password] FROM manager_login_data"

A third option is to tell the command builder to escape every column name, allowing you to continue to use wildcards in your query.第三个选项是告诉命令生成器转义每个列名,允许您继续在查询中使用通配符。 You do this by setting the QuotePrefix and QuoteSuffix properties.您可以通过设置QuotePrefixQuoteSuffix属性来完成此操作。 As you can see from the above SQL example, the prefix is an opening bracket and the suffix is a closing bracket.从上面的 SQL 示例中可以看出,前缀是左括号,后缀是右括号。 Some other databases may use different characters, eg MySQL uses a grave for both.其他一些数据库可能使用不同的字符,例如 MySQL 对两者都使用一个坟墓。 Eg例如

With cb
    .QuotePrefix = "["
    .QuoteSuffix = "]"
End With

You only need to do that once for each command builder and you must do it before calling Update on the data adapter.您只需为每个命令生成器执行一次,并且必须在对数据适配器调用Update之前执行此操作。 The most sensible option is to do it where you create the command builder, eg最明智的选择是在创建命令生成器的地方进行,例如

Dim cb As New OleDbCommandBuilder(da) With {.QuotePrefix = "[",
                                            .QuoteSuffix = "]"}

You are hitting the database twice, once with the .Fill method and once with the .Update .您两次访问数据库,一次使用.Fill方法,一次使用.Update All the overhead of DataAdapters, DataSets, DataTables and CommandBuilders is not necessary. DataAdapters、DataSets、DataTables 和 CommandBuilders 的所有开销都是不必要的。 If all you want to do is add one user then the following steps will accomplish this.如果您只想添加一个用户,那么以下步骤将完成此操作。

  1. Create a connection passing the connection string to the constructor.创建一个将连接字符串传递给构造函数的连接。

  2. Create a command passing the command text and the connection to the constructor.创建一个命令,将命令文本和连接传递给构造函数。

  3. Create the parameters passing the parameter name, datatype and field size to the .Add method and set the values.创建将参数名称、数据类型和字段大小传递给 .Add 方法的参数并设置值。 I had to guess the datatype and field size.我不得不猜测数据类型和字段大小。 Consult the database for the real values and adjust the code accordingly.查询数据库以获取真实值并相应地调整代码。

  4. Open the connection and execute the command.打开连接并执行命令。

I did escape password with brackets in the Sql statement as indicated by @jmcilhinney in his answer.正如@jmcilhinney 在他的回答中所指出的那样,我确实在 Sql 语句中用括号转义了密码。

Private Sub InsertNewUser()
    Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\POS DATABASE.mdb"),
            cmd As New OleDbCommand("Insert Into manager_login_data (username, [password]) Values (?,?);", con)
        With cmd.Parameters
            .Add("@user", OleDbType.VarChar, 100).Value = TextBox1.Text
            .Add("@password", OleDbType.VarChar, 100).Value = TextBox2.Text
        End With
        con.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

EDIT编辑

Try putting a DataGridView on a Form and run the following code after InsertNewUser has been executed.尝试将DataGridView放在Form ,并在执行InsertNewUser后运行以下代码。

Private Sub FillGrid()
    Dim dt As New DataTable
    Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\POS DATABASE.mdb"),
        cmd As New OleDbCommand("Select * From manager_login_data;")
        con.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    DataGridView1.DataSource = dt
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    FillGrid()
End Sub

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

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