VB.NET数据库事务未转到数据库
我正在用VB.NET编写一个销售点系统的代码(在VB.NET中没有其他选择),我试图将其保存到一个数据库中,其中包含收据详细信息等。但是,每当我运行代码时,不会出现错误,它只是不会将数据保存到数据库中 有人知道为什么这不起作用吗。我想这可能是数据库中权限的错误?我试过在连接字符串上乱搞,但运气不好VB.NET数据库事务未转到数据库,vb.net,Vb.net,我正在用VB.NET编写一个销售点系统的代码(在VB.NET中没有其他选择),我试图将其保存到一个数据库中,其中包含收据详细信息等。但是,每当我运行代码时,不会出现错误,它只是不会将数据保存到数据库中 有人知道为什么这不起作用吗。我想这可能是数据库中权限的错误?我试过在连接字符串上乱搞,但运气不好 Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click Dim MyCo
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Dim MyConnection As OleDb.OleDbConnection = Nothing
Dim MyTransaction As OleDb.OleDbTransaction = Nothing
Try
' create the connection and transaction object
MyConnection = New OleDb.OleDbConnection(My.Settings.stock_and_pricesConnectionString)
MyConnection.Open()
MyTransaction = MyConnection.BeginTransaction
' insert the new recipt
Dim SQL As String = "insert into recipts (reciptdate,recipttotal) values (:0,:1)"
Dim CMD1 As New OleDb.OleDbCommand
CMD1.Connection = MyConnection
CMD1.Transaction = MyTransaction
CMD1.CommandText = SQL
CMD1.Parameters.AddWithValue(":0", Now.Date)
CMD1.Parameters.AddWithValue(":1", txtTotal.Text)
CMD1.ExecuteNonQuery()
CMD1.Dispose()
' get the id for the recipt
SQL = "select max(reciptid) as MAXID from recipts"
Dim CMD2 As New OleDb.OleDbCommand
CMD2.Connection = MyConnection
CMD2.Transaction = MyTransaction
CMD2.CommandText = SQL
Dim ReciptID As Long = CMD2.ExecuteScalar()
CMD2.Dispose()
' insert the details of the recipt
Dim I As Integer
For I = 0 To DGV2.Rows.Count - 1
' get the values
Dim Barcode As String = DGV2.Rows(I).Cells(0).Value
Dim BuyPrice As Decimal = DGV2.Rows(I).Cells(2).Value
Dim ItemCount As Integer = DGV2.Rows(I).Cells(3).Value
' next create a command
Dim CMD3 As New OleDb.OleDbCommand
SQL = "insert into ReciptDetails " &
"(reciptid,itemid,itemcount,itemprice) " &
"values " &
"(:0 ,:1 ,:2 ,:3 )"
CMD3.Connection = MyConnection
CMD3.Transaction = MyTransaction
CMD3.CommandText = SQL
CMD3.Parameters.AddWithValue(":0", ReciptID)
CMD3.Parameters.AddWithValue(":1", Barcode)
CMD3.Parameters.AddWithValue(":2", ItemCount)
CMD3.Parameters.AddWithValue(":3", BuyPrice)
CMD3.ExecuteNonQuery()
CMD3.Dispose()
Next
' all well save the changes
MyTransaction.Commit()
' close connection
MyTransaction.Dispose()
MyConnection.Close()
MyConnection.Dispose()
DGV2.Rows.Clear()
Catch ex As Exception
If MyTransaction IsNot Nothing Then
MyTransaction.Rollback()
End If
MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly)
End Try
End Sub
你用过调试器吗?当您在事件处理程序的开始处添加断点时,当您按下该按钮时,执行是否会在该断点处停止?如果是这样的话,
My.Settings.stock\u和\u pricesConnectionString
是否真的会生成到您希望写入结果的数据库的连接字符串?(这些提示可能看起来很奇怪,但在过去,我个人曾多次遇到这样的情况:由于控件名称混乱而调用了“错误”的事件处理程序,或者数据库连接字符串指向错误的数据库。)