答案 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
从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