0

我正在使用 Here 的 ado.net 助手。我不知道如何使用助手使用事务。下面是我尝试过的代码。我在做正确的事吗?我总是遇到This SqlTransaction has completed; it is no longer usable.错误。

            Adodb.ConnectionString = "...";

            Adodb db = new Adodb(); 
            SqlTransaction trans = db.BeginTransaction();

            try
            {
                string qry = "UPDATE PSCHCounter SET SeqNo = '0' WHERE CountID = 'PCSTL'";
                db.ExecNonQuery(qry);
                string qry1 = "UPDATE PSCHCounter SET SeqNo = '1' WHERE CountID = 'GJNLP'";
                db.ExecNonQuery(qry1);

                // Commit 
                trans.Commit();
            }
            catch (Exception ex)
            {
                try
                {
                    // Rollback
                    trans.Rollback();
                    // Log exception
                }
                catch (Exception ex2)
                {
                    // Log exception
                }
            }
            finally
            {
                // Close db connection
                db.Dispose();
            }

谢谢你。

4

1 回答 1

1

问题是你打电话trans.Commit()而不是db.Commit(). 这对我有用:

    AdoHelper.ConnectionString = "...";

    using (AdoHelper db = new AdoHelper())
    {
        // Start the transaction
        db.BeginTransaction();

        try
        {
            db.ExecNonQuery("UPDATE FeedItems SET Title = 'Test3' WHERE Id = 456");
            db.ExecNonQuery("UPDATE FeedItems SET Title = 'Test4' WHERE Id = 457");

            // Commit  
            db.Commit();
        }
        catch (Exception ex)
        {
            db.Rollback();
        }
    }
于 2012-05-15T01:16:01.693 回答