日期:2014-05-17  浏览次数:20422 次

关于 SqlTransaction 的问题
C# code


sqlhelper:
public SqlTransaction SqlTransaction
            {
                get { return _SqlTransaction; }
                private set { _SqlTransaction = value; }
            }

public SqlCommand GetTranSqlCommand(string SqlDBConnectionString)
            {
                SqlConnection SqlConnection = OpenSqlDBConnection(SqlDBConnectionString);
                SqlCommand SqlCommand = null;
                SqlCommand = SqlConnection.CreateCommand();
                [b]this.SqlTransaction = SqlConnection.BeginTransaction();
                SqlCommand.Transaction = this.SqlTransaction;[/b]
                Object TimeOut = null;
                int SqlCommandTimeOut = 1800;
                TimeOut = ConfigurationManager.AppSettings["CommandTimeOut"];
                if (TimeOut != null)
                {
                    SqlCommandTimeOut = Convert.ToInt32(TimeOut);
                }
                SqlCommand.CommandTimeout = SqlCommandTimeOut;

                //CloseSqlDBConnection(SqlConnection);

                return SqlCommand;
            }
private bool TranSqlCommadRollback(SqlCommand SqlCommand)
            {
                try
                {
                    //注:在.NET 4.0 中,如何执行发生异常之后,SqlCommand.Transaction会变成null。??
                    //但是此在.NET 1.1中,却是正常的。
                    SqlCommand.Transaction.Rollback();  执行
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    EndSqlCommad(SqlCommand);
                    SqlCommand.Connection = null;
                    SqlCommand = null;
                }
            }
public bool TranSqlCommadRollback(SqlCommand SqlCommand, SqlTransaction SqlTransaction)
            {
                try
                {
                    SqlTransaction.Rollback();
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    EndSqlCommad(SqlCommand);
                    SqlCommand.Connection = null;
                    SqlCommand = null;
                }
            }

public bool ExecuteNonQuery(SqlCommand SqlCommand, CommandType CommandType, String SqlStringOrSPName, SqlParameter[] paras)
            {
                bool flag = false;
                SqlCommand.CommandType = CommandType;
                SqlCommand.CommandText = SqlStringOrSPName;
                SqlCommand.Parameters.AddRange(paras);
                SqlCommand.ExecuteNonQuery();
                SqlCommand.Parameters.Clear();
                flag = true;
                return flag;
            }



C# code

功能:
public string SaveReviseInfo(XmlDocument xmldoc)
        {
            SqlCommand sqlcommand = sqlhelper.GetTranSqlCommand(sqlhelper.SqlDBConnectionString);
            try
            {
                XmlNamespaceManager nsmgr = XmlHelper.GetNamespaceManager_rsz(xmldoc.NameTable);
                XmlElement lEm;
                XmlNodeList RowNodes;
                RowNodes = xmldoc.SelectNodes("//root/ReviseInfo", nsmgr);

                string SqlStr = "";
                string EditMode = "";
                SqlParameter[] paras = null;
                for (int i = 0; i < RowNodes.Count; i++)
                {
                    lEm = (XmlElement)RowNodes[i];
                    //EditMode=0:初始(加载数据库原有资料行) 1:新增 2:修改 3:删除
                    EditMode = lEm.GetAttribute("EditMode");
                    if (EditMode =