该命令的Transaction属性尚未初始化

时间:2016-02-12 07:07:34

标签: c# transactions ado.net executenonquery

我正在尝试使用事务运行ExecuteNonQuery,但我不断收到此错误

  

OleDbException:ExecuteNonQuery要求命令具有   分配给命令的连接在a中时的事务   等待本地交易。命令的Transaction属性   尚未初始化。

这是我的代码:问题是什么?

 _dataManager = new DataManager();

        bool bTrance = false;
        _dataManager.BuildConnectionString("server", "MyId", "MyPwd");
        _dataManager.Connect();

        try
        {
            Console.WriteLine("Begin Trans");
            var res = _dataManager.BeginTransaction();              
            if (res)
            {
                bTrance = true;
                Console.WriteLine(" Trans Success");
            }
            Console.WriteLine("Query Executed");
            return _dataManager.ExecuteQuery("call SP_MySp");

        }
        catch (OleDbException objDBExc)
        {
            Console.WriteLine("OleDbException : " + objDBExc.Message);
            if (bTrance)
            {

                _dataManager.RollbackTransaction();

            }

            return false;
        }
        catch (Exception objExc)
        {
            Console.WriteLine("OleDbException : " + objExc.Message);
            if (bTrance)
            {
                bTrance = false;
                _dataManager.RollbackTransaction();

            }

            return false;
        }
        finally
        {
            Console.WriteLine("Done!");
            if (bTrance)
            {
                _dataManager.CommitTransaction();

            }
        }

 public class DataManager
{


    private OleDbConnectionStringBuilder dbConnStr;
    private OleDbConnection dbConn;
    private OleDbTransaction dbTransaction;
    private OleDbCommand dbCommand;
    private bool beginTransaction;


 public bool BeginTransaction()
    {
        try
        {

            if (beginTransaction == false)
            {
                dbTransaction = dbConn.BeginTransaction();
                dbCommand.Transaction = dbTransaction;
                beginTransaction = true;
            }
            else
                dbCommand.Transaction = dbTransaction;
        }
        catch (Exception)
        {
            throw;
        }

        return beginTransaction;
    }

 public bool ExecuteQuery(string command)
    {
        try
        {   
            dbCommand.ExecuteNonQuery();
        }
        catch (OleDbException objDbEx)
        {

            throw;
        }
        catch (Exception objEx)
        {

            throw;
        }

        dbCommand.Parameters.Clear();
        dbCommand.Connection = null;

        return true;
    }

}

1 个答案:

答案 0 :(得分:2)

调用SqlConnection对象的BeginTransaction方法以标记事务的开始。 BeginTransaction方法返回对事务的引用。此引用分配给在事务中登记的SqlCommand对象。

将Transaction对象分配给要执行的SqlCommand的Transaction属性。如果在具有活动事务的连接上执行命令,并且尚未将Transaction对象分配给Command对象的Transaction属性,则会引发异常。

执行所需的命令。

调用SqlTransaction对象的Commit方法来完成事务,或调用Rollback方法中止事务。如果在执行Commit或Rollback方法之前关闭或处置连接,则回滚事务。

参考https://msdn.microsoft.com/en-us/library/2k2hy99x(VS.80).aspx