“INSERT INTO 中的语法错误”ms-access vb.net [重复]

Posted

技术标签:

【中文标题】“INSERT INTO 中的语法错误”ms-access vb.net [重复]【英文标题】:"syntax error in INSERT INTO" ms-access vb.net [duplicate] 【发布时间】:2018-07-06 11:26:04 【问题描述】:
 If connection.Units = Nothing Then
            MetroFramework.MetroMessageBox.Show(Me, "You didnt Choose quantites", "Stop", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop)
            'MetroFramework.MetroMessageBox.Show(Me, "Oops you didnt choose your desired quantites")
        Else
            'Try
            Dim Conn = New OleDb.OleDbConnection
            Dim str As String = "Select * From Cars"
            ' Dim dataAdapter As New OleDbDataAdapter(str, Conn)
            Dim dataSet As New DataSet()
            With Conn
                .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Stark\Documents\LaShop.mdb"
                .Open()
            End With
            Dim Sql As String
            Sql = "Insert Into Transactions (TransctID,TotalPrice,Username,Time,Discount) Values(@TranID,@price,@uname,@time,@Dsc)"
            'Dim cm = New OleDbCommand(Sql, Conn)
            'Dim cn = New OleDb.OleDbConnection
            Dim TranID As String
            Dim nd As New Random
            TranID = nd.Next & connection.Values(0)
            Using cm As New OleDbCommand(Sql, Conn)
                cm.Connection = Conn
                cm.CommandText = CommandType.Text
                cm.CommandText = Sql
                '.CommandText = "Insert Into Items (ItemID,BrandName,Category,ItemPrice,ItemName,Quantity)Values(@ID,@Bname,@cat,@price,@ItemName,@qty)"
                cm.Parameters.AddWithValue("@TranID", TranID)
                cm.Parameters.AddWithValue("@price", connection.TotalPrice)
                cm.Parameters.AddWithValue("@uname", connection.uname)
                cm.Parameters.AddWithValue("@time", TimeOfDay)
                cm.Parameters.AddWithValue("@Dsc", connection.DscAmount)

                cm.ExecuteNonQuery()
                ' MetroFramework.MetroMessageBox.Show(Me, "Your data is entered and saved")
            End Using
            Dim Dpt As New OleDbDataAdapter
            Dpt = New OleDbDataAdapter("Select Sum(TotalPrice) From Cart", Conn)
            'Dim Dpt As New OleDbDataAdapter("Select * From Items where ItemID=@I", Conn)
            'Cmd.Parameters.AddWithValue("@I", Str)
            Dim ds1 As New DataSet
            Dpt.Fill(ds1, "tp")
            'Dsctext.DataSource = ds1
            'Dsctext.DataMember = "tp"
            Results.tp.Text = ds1.Tables(0).Rows(0).Item(0)
            Dim ds As New DataSet
            Dim DataAdapter2 As New OleDbDataAdapter("select * From Cart", Conn)
            DataAdapter2.Fill(ds, "cart")
            Res1Grid.DataSource = ds
            Res1Grid.DataMember = "cart"

        End If
    End Sub

【问题讨论】:

阅读此OleDbCommand.Parameters Property 以获取有关基于 ParametersOleDb 的客户端的一些指导 鉴于您正在使用参数,最可能的原因似乎是使用了保留字。尝试将列名和表名括在括号中。 【参考方案1】:

当您只需发布受影响语句中使用的错误行和变量时,无需发布所有代码。其余的只是混淆

正如评论者 @jmcilhinney 所说 - 查找保留字并用括号括起来

假设您已将 TransactID 正确拼写为 TransctID,那么在您的情况下,显而易见的选择是字段名称 Time。用方括号括起来

Sql = "Insert Into Transactions (TransctID,TotalPrice,Username,[Time],Discount) Values(@TranID,@price,@uname,@time,@Dsc)"

【讨论】:

【参考方案2】:

这个怎么样?

插入 MS Access:

Imports System.Data.OleDb

Public Class Form1

    Private ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Excel\Desktop\Coding\Microsoft Access\Northwind.mdb;"
    Private NewIdentifer As Integer = 0
    Private InsertStatement As String = "INSERT INTO Employee (LName) Values(@LName)"
    Private IdentifierStatement As String = "Select @@Identity"

    'Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Using cn As New OleDbConnection(ConnectionString)
            Using cmd As New OleDbCommand("SELECT * FROM Employee", cn)
                Dim dt As New DataTable
                cn.Open()
                Dim Reader As OleDbDataReader = cmd.ExecuteReader()
                dt.Load(Reader)
                Dim dv = dt.DefaultView
                DataGridView1.DataSource = dv
            End Using
        End Using
    End Sub

    'End Sub

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

        If Not String.IsNullOrEmpty(txtLastName.Text) Then
            Using cn As New OleDbConnection(ConnectionString)
                Using cmd As New OleDbCommand(InsertStatement, cn)
                    cmd.Parameters.AddWithValue("@LName", txtLastName.Text)
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    cmd.CommandText = IdentifierStatement
                    NewIdentifer = CInt(cmd.ExecuteScalar())
                    Dim Row As DataRowView = CType(DataGridView1.DataSource, DataView).AddNew
                    Row("Fname") = NewIdentifer
                    Row("LName") = txtLastName.Text
                    Row.EndEdit()
                    DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.RowCount - 1)
                    txtLastName.Text = ""
                End Using
            End Using
        Else
            MsgBox("Please enter a name")
        End If

    End Sub
End Class

从 DataGridView 插入 MS Access:

Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class Form1
    Dim myDA As OleDbDataAdapter
    Dim myDataSet As DataSet

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

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\Ryan\Desktop\Coding\Microsoft Access\Powerful Access Files\Nwind.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Customers", con)
        con.Open()
        myDA = New OleDbDataAdapter(cmd)
        'Automatically generates DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object
        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)

        myDataSet = New DataSet()

        myDA.Fill(myDataSet, "MyTable")
        DataGridView2.DataSource = myDataSet.Tables("MyTable").DefaultView

        con.Close()
        con = Nothing
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Validate()
        Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
    End Sub

End Class

【讨论】:

这与 OP 的 INSERT 语法错误有什么关系?

以上是关于“INSERT INTO 中的语法错误”ms-access vb.net [重复]的主要内容,如果未能解决你的问题,请参考以下文章