解决方案 »

  1.   

    参考:public ExecuteResult ExecuteScalar(SqlCommand sqlCommand)
    {
    ChangeNullToDBNullValue(sqlCommand);
    bool useDefaultConnection = false;
    if (sqlCommand.Connection == null)
    {
    useDefaultConnection = true;
    sqlCommand.Connection = new SqlConnection(this.connectionString);
    }
    else
    {
    useDefaultConnection = false;
    if (sqlCommand.Connection.State != ConnectionState.Closed)
    {
    throw new ArgumentException("SqlCommand's connection state must be closed.");
    }
    }
    sqlCommand.Connection.Open();
    sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
    try
    {
    object returnValue = sqlCommand.ExecuteScalar();
    sqlCommand.Transaction.Commit();
    return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = returnValue };
    }
    catch (SqlException ex)
    {
    sqlCommand.Transaction.Rollback();
    DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
    return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
    }
    finally
    {
    sqlCommand.Connection.Close();
    if (useDefaultConnection)
    {
    sqlCommand.Connection = null;
    }
    }
    }
    private void ChangeNullToDBNullValue(SqlCommand command)
    {
        foreach (SqlParameter para in command.Parameters)
        {
            if (para.Value == null)
            {
                para.Value = DBNull.Value;
            }
            else
            {
                if (para.Value is string)
                {
                    if (string.IsNullOrWhiteSpace(para.Value.ToString()))
                    {
                        para.Value = string.Empty;
                    }
                }
            }
        }
    }可能没有人象你那样写,没有符合条件就回滚。一般都是没有错误都提交,不管有没有符合条件的记录。
    还有insert into语句插入成功后,都有返回值select @newID
      

  2.   


    是不是没有理解对?
    我跟踪了代码,而且执行了 事务的Rollback()方法,而数据库没有进行回滚
      

  3.   


    你可以不看try里面的判断语句,而直接看catch。
    当代码进入catch的时候,并且执行了Rollback()方法,而数据库没有进行回滚。
    我遇到的问题是这个。
    执行Rollback()方法,数据库没反应
      

  4.   

    你这会回滚在有鬼了。
    SqlLibrary.SqlHelper.ExecuteNonQuery(DAL.CONN_STRING, CommandType.Text, sql);
    sc在哪?sc根本没有用上,回滚什么啊?
      

  5.   

     i = SqlLibrary.SqlHelper.ExecuteNonQuery(DAL.CONN_STRING, CommandType.Text, sql);
    这个应该是你自己封装的command吧!你都没有把Transaction带入进command,怎么执行事务呢!
    把方法重写下吧
      

  6.   

    两个数据库连接,一个开了事务没去执行任何SQL语句,另一个没开事务去执行了你的sql语句了。