【问题标题】:I have an error with the INSERT INTO statement我的 INSERT INTO 语句有错误
【发布时间】:2012-11-09 15:24:49
【问题描述】:

这是我的代码:

Imports System.Data
Imports System.Data.OleDb

Public Class frmAdd
Dim con As New OleDbConnection
Dim com As New OleDbCommand
Dim ins As New OleDbCommand
Dim upd As New OleDbCommand
Dim strcon = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = Supplies.mdb")

Private Sub frmAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = strcon
    con.Open()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    ins.CommandText = "INSERT INTO [product info] ([Product Name:],[Description:],[Quantity:],[Type:],[Date Received:],[Barcode:],[Price:]) VALUES ('" & txtItemName.Text & "', '" & txtDescription.Text & "', " & txtItemCount.Text & ", '" & cmbItemType.Text & "', " & txtDate.Text & ", '" & txtBarcode.Text & "', '" & txtPrice.Text & "',);"
    ins.Parameters.AddWithValue("@name", txtItemName.Text)
    ins.Parameters.AddWithValue("@desc", txtDescription.Text)
    ins.Parameters.AddWithValue("@count", Convert.ToInt32(txtItemCount.Text))
    ins.Parameters.AddWithValue("@type", cmbItemType.Text)
    ins.Parameters.AddWithValue("@dt", Convert.ToDateTime(txtDate.Text))
    ins.Parameters.AddWithValue("@code", txtBarcode.Text)
    ins.Parameters.AddWithValue("@price", txtPrice.Text)

    ins.CommandType = CommandType.Text
    ins.Connection = con
    ins.ExecuteNonQuery()
    ins.Dispose()

    con.Close()

填满所有文本框后,我点击保存按钮,当我点击保存按钮时出现错误“INSERT INTO 语句中的语法错误。”

【问题讨论】:

    标签: vb.net winforms ms-access insert


    【解决方案1】:

    带空格的表名应该用方括号括起来(产品信息的名称中有空格),列名也是如此(产品名称,接收日期)。
    然后,如果您的所有列名中确实有 :,则在任何地方使用方括号,否则从 sql 文本(和数据库字段)中删除 :

    ins.CommandText = "INSERT INTO [product info] ([Product Name:], [Description:], " + 
                     "[Quantity:],[Type:],[Date Received:],[Barcode:],[Price:]) " + 
                     "VALUES (?,?,?,?,?,?,?)"
    

    也就是说,永远不要使用字符串连接来构建一个sql文本传递给数据库引擎。
    您避免了日期和文本解析的问题(例如,如果您的输入文本中包含引号,一切都会失败),而且您 avoid Sql Injection Attacks

    ins.Parameters.AddWithValue("@name", txtItemName.Text)
    ins.Parameters.AddWithValue("@desc", txtDescription.Text)
    ins.Parameters.AddWithValue("@count", Convert.ToInt32(txtItemCount.Text))
    ins.Parameters.AddWithValue("@type", cmbItemType.Text)
    ins.Parameters.AddWithValue("@dt", Convert.ToDateTime(txtDate.Text)
    ins.Parameters.AddWithValue("@code", txtBarcode.Text)
    ins.Parameters.AddWithValue("@price", Convert.ToDecimal(txtPrice.Text)
    

    我假设 Quantity 是一个数字列,Date Received 是一个 DateTime 列,Price 是一个数字列。

    【讨论】:

    • 列名中的冒号是故意的。我编辑了问题中的查询,请检查是否仍有错误。谢谢
    • 不,仍然有错误:[Barcode:,Price:] 应该是 [Barcode:],[Price:],并从 VALUES(.....) 中删除您的文本并使用?占位符
    • 通常发生这种情况是因为每次启动程序时都会用新副本覆盖工作数据库。请记住,当您在调试/发布模式下运行时,您的工作目录是 project/bin/debug 或 release。根据您的连接字符串,数据库应该位于此处并由您的代码更新,但是,如果您将 MDB 文件的属性复制到输出目录设置为始终复制,那么您将失去更改。
    • 是的,创建一个新文件夹并设置连接字符串以使用该文件夹中的数据库。例如,创建类似此路径的内容并将连接字符串中的数据源设置为C:\Projects\MyProject\Database\supplies.mdb。您应该从配置文件的 AppSettings 中读取此值,以便在将应用程序安装到其他系统时更新路径
    • 不,但是在您的应用程序中搜索您使用上述连接字符串的任何地方,并将其替换为从文件配置support.microsoft.com/kb/815786/en-us中的 AppSettings 中读取信息
    【解决方案2】:

    每列名称后面的 : 有什么用,请随时在 cmets 中解释。我的猜测是它是错误的......

    " & txtItemName.Text & "是文本,需要用'封装。

    " & cmbItemType.Text & "是文本,需要封装在'中。

    " & txtDate.Text & " 需要转换成日期。

    " & txtBarcode.Text & " 需要封装在 ' 中,因为它是文本。

    ",);" 最后一个逗号需要去掉。

    我不完全确定您可以在表名和/或列名中包含空格,请随时对此发表评论。

    然而,我的猜测是,这是某种学校任务,因为这个查询的错误比正确的要多...

    【讨论】:

    • 我编辑了我的问题,请再次检查错误。请原谅我使用的代码,因为我只是新手。
    • 按照史蒂夫所说的去做。他比我更了解这一点。
    【解决方案3】:
    ins.CommandText = "INSERT INTO product info (Product Name:,Description:,Quantity:,Type:,Date Received:,Barcode:,Price:) VALUES (" & txtItemName.Text & ", '" & txtDescription.Text & ", " & txtItemCount.Text & ", " & cmbItemType.Text & ", " & txtDate.Text & ", " & txtBarcode.Text & ", " & txtPrice.Text & ",);"
    

    请更正您的陈述,即您的查询中有一个额外的撇号 (')。 只需删除那个撇号。我想我会工作得很好

    【讨论】:

      【解决方案4】:

      您的插入代码:

        ins.CommandText = "INSERT INTO product info (Product Name:,Description:,Quantity:,Type:,Date Received:,Barcode:,Price:) VALUES (" & txtItemName.Text & ", '" & txtDescription.Text & ", " & txtItemCount.Text & ", " & cmbItemType.Text & ", " & txtDate.Text & ", " & txtBarcode.Text & ", " & txtPrice.Text & ",);"
      

      在列之间有“:”。不知道是不是故意的。

      另外,您的 insert into 语句是错误的。应该是这样的:

      INSERT INTO ProductInfo(ProductName, Description, Quantity,.....etc)
      

      如果表格确实有空格即产品信息,不确定如何处理,可能使用[产品信息]

      最后确保从控件获取值时insert into语句中没有非法字符。

      也许考虑使用 SP 并将值参数化或清理输入。

      只是一个想法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多