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

时间:2018-07-06 11:26:04

标签: sql-server vb.net ms-access oledb

 如果connection.Units = Nothing然后             MetroFramework.MetroMessageBox.Show(我,“您没有选择数量”,“停止”,MessageBoxButtons.OKCancel,MessageBoxIcon.Stop)             'MetroFramework.MetroMessageBox.Show(我,“糟糕,您没有选择所需的数量”)         其他             '尝试             Dim Conn =新的OleDb.OleDbConnection             Dim str As String =“选择*来自汽车”             'Dim dataAdapter作为新的OleDbDataAdapter(str,Conn)             将dataSet设置为New DataSet()             与康恩                 .ConnectionString =“ Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\ Users \ Stark \ Documents \ LaShop.mdb”                 。打开()             结束于             昏暗的SQL作为字符串             Sql =“将交易插入(TransctID,TotalPrice,用户名,时间,折扣)值(@ TranID,@ price,@ uname,@ time,@ Dsc)”             'Dim cm = New OleDbCommand(Sql,Conn)             'Dim cn =新的OleDb.OleDbConnection             Dim TranID作为字符串             暗淡为新随机             TranID = nd.Next&connection.Values(0)             使用cm作为新的OleDbCommand(Sql,Conn)                 cm.Connection =连接                 cm.CommandText = CommandType.Text                 cm.CommandText = SQL                 '.CommandText =“将项目(项目ID,品牌名称,类别,项目价格,项目名称,数量)插入值(@ 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(我,“您的数据已输入并保存”)             最终使用             昏暗的Dpt作为新的OleDbDataAdapter             Dpt =新的OleDbDataAdapter(“从购物车中选择总和(总价)”,康恩)             '将Dim Dpt作为新的OleDbDataAdapter(“选择*从其中ItemID = @ I的项目中选择”,Conn)             'Cmd.Parameters.AddWithValue(“ @ I”,Str)             Dim ds1作为新数据集             Dpt.Fill(ds1,“ tp”)             'Dsctext.DataSource = ds1             'Dsctext.DataMember =“ tp”             Results.tp.Text = ds1.Tables(0).Rows(0).Item(0)             将Dim ds作为新数据集             昏暗的DataAdapter2作为新的OleDbDataAdapter(“选择*从购物车”,康恩)             DataAdapter2.Fill(ds,“ cart”)             Res1Grid.DataSource = ds             Res1Grid.DataMember =“购物车”         万一     结束子

2 个答案:

答案 0 :(得分:1)

仅需发布错误行和受影响的语句中使用的变量时,无需发布所有代码。其余的只是使人困惑

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

假设您已经正确地将TransactID拼写为TransctID,那么在您的情况下,显而易见的选择是字段名Time。将其包裹在方括号中

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

答案 1 :(得分:0)

这个怎么样?

插入MS Access:

all

enter image description here

从DataGridView插入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

enter image description here