尝试“交易".首次... [英] Trying a "transaction" for the first time...

查看:63
本文介绍了尝试“交易".首次...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试使用交易...只是想知道我是否正确"完成了?看来还可以,但这并不代表什么.

只是尝试从数据表更新Access数据库.

This is the first time I''ve tried using a transaction... just wondering if I have done it "right"? It seems to work ok, but that doesn''t mean anything.

Just trying to update an Access database from a datatable.

objConn.openConnection()
        Dim myTrans As OleDbTransaction = objConn.con.BeginTransaction()
        Dim cmd As New OleDbCommand
        cmd.Connection = objConn.con
        cmd.Transaction = myTrans
        Dim tRow As DataRow
        Dim sql As String

        Try
            For Each tRow In table.Rows
                sql = String.Format("UPDATE Pricing SET unitPrice = {0}, salesPrice = {1}, ProfitMargin = {2} WHERE ItemID = {3}", tRow(1), tRow(2), tRow(3), tRow(0))
                cmd.CommandText = sql
                cmd.ExecuteNonQuery()
            Next
            myTrans.Commit()
            MessageBox.Show("Update Complete", "Vending", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.ToString & vbCrLf & "Un-doing any work that has been done!", "Vending", MessageBoxButtons.OK, MessageBoxIcon.Error)
            myTrans.Rollback()
        Finally
            objConn.closeConnection()
        End Try

推荐答案

这已正确实施.如果要测试事务,请至少有2个表要在其中插入数据,并在将数据插入1个表后调用异常.这将回滚数据,而您不会在表中插入部分数据.这就是实现事务时的期望.
This is implemented properly. If you want to test transaction, have atleast 2 tables where you intend to insert data and invoke an exception after data in inserted in 1 table. That would rollback the data and you don''t get partial data inserted in tables. This is what you expect when you implement transaction.


这篇关于尝试“交易".首次...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆