C#事务

1、创建事务的结构
[csharp]  view plain copy print ?
  1. SqlConnection   sqlConnection   =   new   SqlConnection();     
  2.   //  初始化连接     
  3.   //   开启事务     
  4.   SqlTransaction   sqlTransaction   =   sqlConnection.BeginTransaction();     
  5.   //   将事务应用于Command     
  6.   SqlCommand   sqlCommand   =   new   SqlCommand();     
  7.   sqlCommand.Connection   =   sqlConnection;     
  8.   sqlCommand.Transaction   =   sqlTransaction;       
  9.   try     
  10.   {     
  11.   //   利用sqlcommand进行数据操作        
  12.   //   成功提交     
  13.   sqlTransaction.Commit();     
  14.   }     
  15.   catch(Exception   ex)     
  16.   {     
  17.   //   出错回滚     
  18.   sqlTransaction.Rollback();     
  19.   }    
2、简单例子
[csharp]  view plain copy print ?
  1. {     
  2.         DataTable   dt   =   new   DataTable();     
  3.         System.Data.SqlClient.SqlConnection   cnn= new  System.Data.SqlClient.SqlConnection("连接字符串");     
  4.         System.Data.SqlClient.SqlCommand   cm   =   new   System.Data.SqlClient.SqlCommand();     
  5.         cm.Connection   =   cnn;     
  6.         cnn.Open();     
  7.         System.Data.SqlClient.SqlTransaction   trans   =   cnn.BeginTransaction();     
  8.         try     
  9.         {     
  10.                 foreach(DataRow   dr   in   dt.Rows)     
  11.                 {    
  12.                      cm.CommandText   = "update [表] set [数量] = @amount where   productID   =   @productID";     
  13.                         cm.Parameters.Add("@amount",SqlDbType.Int);     
  14.                         cm.Parameters["@amount"].Value   =   Convert.ToInt32(dr["amount"]);     
  15.                         cm.Parameters.Add("@productID",SqlDbType.VarChar);     
  16.                         cm.Parameters["@productID"].Value   =   dr["productID"].ToString();     
  17.                         cm.ExecuteNonQuery();     
  18.                 }     
  19.                 trans.Commit();     
  20.         }     
  21.         catch     
  22.         {     
  23.                 trans.Rollback();     
  24.         }     
  25.         finally     
  26.         {     
  27.                 cnn.Close();     
  28.                 trans.Dispose();     
  29.                 cnn.Dispose();     
  30.         }     
  31. }  
3、SQl server中的事务例子
[csharp]  view plain copy print ?
  1. begin transaction   
  2. save transaction A  
  3. insert into demo values('BB','B term')  
  4. rollback TRANSACTION A  
  5. create table demo2(name varchar(10),age int)  
  6. insert into demo2(name,age) values('lis',1)  
  7. rollback transaction  
  8.   insert into demo values('BB','B term')  
  9.  commit TRANSACTION A  
  10.  commit TRANSACTION    

4、注意
  1。事务必须在连接打开后BeginTransaction();
  2.事务添加到SqlCommand(sqlCommand.Transaction   =   sqlTransaction;   )
  3、其他数据库对应做相应调整
  4、可以用微软提供的一个dll,很方便
绝对能看能用的C#代码 using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace NetAddressCollector { public class CDataAccess : IDataAccess { #region IDataAccess 成员 private ArrayList _classList; public ArrayList ClassList { get { return _classList; } } private ArrayList _addressList; public ArrayList AddressList { get { return _addressList; } } private bool _isDirty = false; public bool IsDirty { get { return _isDirty; } } public void LoadData() { _classList = new ArrayList(); _addressList = new ArrayList(); string content = CFileOperation.ReadFile(CConst.DATAFILE); if (content.Length < 1) return; string[] ss1 = content.Split(new string[] { CConst.SPLITOR_CLASS_HTTP }, StringSplitOptions.RemoveEmptyEntries); string classString = ss1[0]; string httpString = ss1[1]; string[] ss2 = classString.Split(new string[] { CConst.SPLITOR_CLASS_ITEM }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in ss2) _classList.Add(new CClass(s)); string[] ss3 = httpString.Split(new string[] { CConst.SPLITOR_HTTP_ITEM }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in ss3) { string[] ss4 = s.Split(new string[] { CConst.SPLITOR_HTTP_DETAIL }, StringSplitOptions.RemoveEmptyEntries); foreach (CClass c in _classList) { if (c.Name == ss4[0]) { _addressList.Add(new CAddress(c, ss4[1], ss4[2], ss4[3])); break; } } } _isDirty = false; } public void SaveData() { StringBuilder sbClass = new StringBuilder(); foreach (CClass c in _classList) sbClass.Append(string.Format("{0}{1}", CConst.SPLITOR_CLASS_ITEM, c.Name)); StringBuilder sbContent = sbClass.Append(CConst.SPLITOR_CLASS_HTTP); StringBuilder sbHttp = new StringBuilder(); foreach (CAddress http in _addressList) sbHttp.Append(string.Format("{0}{1}", CConst.SPLITOR_HTTP_ITEM, http.ToString())); sbContent.Append(sbHttp); CFileOperation.WriteFile(CConst.DATAFILE, sbContent.ToString()); _isDirty = false; } public bool ExistClass(CClass httpClass) { foreach (CClass c in _classList) { if (c.Name == httpClass.Name) return true; } return false; } public bool ExistHttp(CAddress http) { foreach (CAddress addr in _addressList) { if (addr.Name == http.Name && addr.Name == http.Http) return true; } return false; } public void AddClass(string className) { CClass c = new CClass(className); AddClass(c); } public void AddClass(CClass httpClass) { _classList.Add(httpClass); _isDirty = true; } public void RemoveClass(CClass httpClass) { for (int i = _addressList.Count - 1; i >= 0; i--) { CAddress http = (CAddress)_addressList[i]; if (http.HttpClass.Name == httpClass.Name) RemoveHttp(http); } _classList.Remove(httpClass); _isDirty = true; } public void AddHttp(CClass httpClass, string name, string http, string remark) { CAddress addr = new CAddress(httpClass, name, http, remark); AddHttp(addr); } public void AddHttp(CAddress http) { _addressList.Add(http); _isDirty = true; } public void RemoveHttp(CAddress http) { _addressList.Remove(http); _isDirty = true; } #endregion } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值