关于 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 == "0") { } else if (EditMode == "1") { } else if (EditMode == "2") { } else if (EditMode == "3") { } SqlStr = "SELECT RowsCount=COUNT(1) FROM [SP_Revise] WHERE [Code]=@Code"; paras = new SqlParameter[] { new SqlParameter("@Code", lEm.GetAttribute("Code")) }; int obj = (int)sqlhelper.ExecuteScalar(sqlcommand, CommandType.Text, SqlStr, paras); if (obj > 0) { SqlStr = "INSERT INTO [SP_Revory] SELECT * FROM [SP_Revise] WHERE [Code]=@Code;DELETE [SP_Revise] WHERE [Code]=@Code"; paras = new SqlParameter[] { new SqlParameter("@Code", lEm.GetAttribute("Code")) }; sqlhelper.ExecuteNonQuery(sqlcommand, CommandType.Text, SqlStr, paras); } SqlStr = "INSERT INTO [SP_Revise]([Code],[CType],[Name],[Location],[Category],[Suppliers],[BuyTime],[Status],[Deploy],[AdminCD],[AdminNM],[Guid],[Modify],[ModifyNM],[NewTime],[UserNo],[UserNM]) VALUES(@Code,@CType,@Name,@Location,@Category,@Suppliers,@BuyTime,@Status,@Deploy,@AdminCD,@AdminNM,@Guid,@Modify,@ModifyNM,@NewTime,@UserNo,@UserNM)"; paras = new SqlParameter[] { new SqlParameter("@Code",lEm.GetAttribute("Code")), new SqlParameter("@CType",lEm.GetAttribute("CType")), new SqlParameter("@Name",lEm.GetAttribute("Name")), new SqlParameter("@Location",lEm.GetAttribute("Location")), new SqlParameter("@Category",lEm.GetAttribute("Category")), new SqlParameter("@Suppliers",lEm.GetAttribute("Suppliers")), new SqlParameter("@BuyTime",lEm.GetAttribute("BuyTime")), new SqlParameter("@Status",lEm.GetAttribute("Status")), new SqlParameter("@Deploy",lEm.GetAttribute("Deploy")), new SqlParameter("@AdminCD",lEm.GetAttribute("AdminCD")), new SqlParameter("@AdminNM",lEm.GetAttribute("AdminNM")), new SqlParameter("@Guid",System.Guid.NewGuid().ToString()), new SqlParameter("@Modify",lEm.GetAttribute("Modify")), new SqlParameter("@ModifyNM",lEm.GetAttribute("ModifyNM")), new SqlParameter("@NewTime",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")), new SqlParameter("@UserNo",lEm.GetAttribute("UserNo")), new SqlParameter("@UserNM",lEm.GetAttribute("UserNM")) }; sqlhelper.ExecuteNonQuery(sqlcommand, CommandType.Text, SqlStr, paras); } //sqlhelper.TranSqlCommadCommit(sqlcommand); sqlhelper.TranSqlCommadCommit(sqlcommand,sqlhelper.SqlTransaction); return "ok|保存成功!"; } catch (Exception ex) { //sqlhelper.TranSqlCommadRollback(sqlcommand);//出现SqlCommand.Transaction会变成null 的现象。 sqlhelper.TranSqlCommadRollback(sqlcommand, sqlhelper.SqlTransaction);//出现 此 SqlTransaction 已完成;它再也无法使用 return "er|" + ex.Message; } }问题:1.在执行SqlStr = "INSERT INTO [SP_Revise]([Code],[CType]....语句时候出现异常后进行回滚,可是却出现SqlCommand.Transaction会变成null 的现象。问题:2.是否不用指明Rollback(),如果有异常的话,事物会自动回滚?
[解决办法]
数据访问的那个类不太对,要改的地方有点多...可能就是都需要重写吧
[解决办法]
1.对象为Null的原因可能是你的事务对象初始化是在Try块里进行的
2.逻辑事务如此,但你的是Sql事务