在C#和ado.net中使用事务
2007-04-11 10:29
525 查看
一般情况
SqlTransaction tn ; //declare a transaction
const string sql = "INSERT INTO Employees1(EmpID) VALUES (@UserID)";
SqlConnection cn = new SqlConnection("data source=AUG-SQLSRV;initial catalog=HumanResources;integrated security=SSPI");
try...{if(cn.State != ConnectionState.Open)...{cn.Open();}}
//If we throw an exception on Open, which is a 'risky' operation
//manually make the assertino fail by setting it to false and use
//ex.ToString() to get the information about the exception.
catch (SqlException ex)...{Debug.Assert(false, ex.ToString());}
//Instantiate command with CommandText and Connection and t //transaction
tn = cn.BeginTransaction();
SqlCommand cmd = new SqlCommand(sql, cn,tn);
cmd.Parameters.Clear();
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = 314;
try
...{
//You can test for records affected, in this case we know it
//would be at most one record.
int i = cmd.ExecuteNonQuery();
//If successful, commit the transaction
//Loop 5 times and just add the id's incremented each time
for(int x=0; x<5; x++)
...{
cmd.Parameters["@UserID"].Value = (315 + x);
cmd.ExecuteNonQuery();
}
cmd.Parameters["@UserID"].Value = (325);
cmd.ExecuteNonQuery();
tn.Commit();
}
catch(SqlException ex)...{
Debug.Assert(false, ex.ToString());
//If it failed for whatever reason, rollback the //transaction
tn.Rollback();
//No need to throw because we are at a top level call and //nothing is handling exceptions
}
finally...{
//Check for close and respond accordingly
if(cn.State != ConnectionState.Closed)...{cn.Close();}
//Clean up my mess
cn.Dispose();
cmd.Dispose();
tn.Dispose();
}
在SQLhelper中使用transaction
public static DataSet GetICContact(string ICID)
...{
SqlConnection conn = GetConnection();
SqlTransaction tn=null ;
try
...{
if (conn.State!=ConnectionState.Open)
...{
conn.Open();
}
tn = conn.BeginTransaction();
SqlParameter[] arParams = new SqlParameter[1];
if (System.Configuration.ConfigurationSettings.AppSettings["CacheMSSQLParameters"].ToLower() == "true")
...{
arParams = SqlHelperParameterCache.GetSpParameterSet(GetConnectionString(),
"usp_IC_Contact");
arParams[0].Value = ICID;
}
else
...{
arParams[0] = new SqlParameter("@ICID", SqlDbType.NVarChar, 50);
arParams[0].Direction = ParameterDirection.Input;
arParams[0].Value = ICID;
}
DataSet ds = SqlHelper.ExecuteDataset(tn, CommandType.StoredProcedure, "usp_IC_Contact", arParams);
tn.Commit();
return ds;
}
catch (System.Exception e)
...{
tn.Rollback();
return null;
}
finally
...{
if(conn.State != ConnectionState.Closed)...{conn.Close();}
conn.Dispose();
tn.Dispose();
}
}
SqlTransaction tn ; //declare a transaction
const string sql = "INSERT INTO Employees1(EmpID) VALUES (@UserID)";
SqlConnection cn = new SqlConnection("data source=AUG-SQLSRV;initial catalog=HumanResources;integrated security=SSPI");
try...{if(cn.State != ConnectionState.Open)...{cn.Open();}}
//If we throw an exception on Open, which is a 'risky' operation
//manually make the assertino fail by setting it to false and use
//ex.ToString() to get the information about the exception.
catch (SqlException ex)...{Debug.Assert(false, ex.ToString());}
//Instantiate command with CommandText and Connection and t //transaction
tn = cn.BeginTransaction();
SqlCommand cmd = new SqlCommand(sql, cn,tn);
cmd.Parameters.Clear();
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = 314;
try
...{
//You can test for records affected, in this case we know it
//would be at most one record.
int i = cmd.ExecuteNonQuery();
//If successful, commit the transaction
//Loop 5 times and just add the id's incremented each time
for(int x=0; x<5; x++)
...{
cmd.Parameters["@UserID"].Value = (315 + x);
cmd.ExecuteNonQuery();
}
cmd.Parameters["@UserID"].Value = (325);
cmd.ExecuteNonQuery();
tn.Commit();
}
catch(SqlException ex)...{
Debug.Assert(false, ex.ToString());
//If it failed for whatever reason, rollback the //transaction
tn.Rollback();
//No need to throw because we are at a top level call and //nothing is handling exceptions
}
finally...{
//Check for close and respond accordingly
if(cn.State != ConnectionState.Closed)...{cn.Close();}
//Clean up my mess
cn.Dispose();
cmd.Dispose();
tn.Dispose();
}
在SQLhelper中使用transaction
public static DataSet GetICContact(string ICID)
...{
SqlConnection conn = GetConnection();
SqlTransaction tn=null ;
try
...{
if (conn.State!=ConnectionState.Open)
...{
conn.Open();
}
tn = conn.BeginTransaction();
SqlParameter[] arParams = new SqlParameter[1];
if (System.Configuration.ConfigurationSettings.AppSettings["CacheMSSQLParameters"].ToLower() == "true")
...{
arParams = SqlHelperParameterCache.GetSpParameterSet(GetConnectionString(),
"usp_IC_Contact");
arParams[0].Value = ICID;
}
else
...{
arParams[0] = new SqlParameter("@ICID", SqlDbType.NVarChar, 50);
arParams[0].Direction = ParameterDirection.Input;
arParams[0].Value = ICID;
}
DataSet ds = SqlHelper.ExecuteDataset(tn, CommandType.StoredProcedure, "usp_IC_Contact", arParams);
tn.Commit();
return ds;
}
catch (System.Exception e)
...{
tn.Rollback();
return null;
}
finally
...{
if(conn.State != ConnectionState.Closed)...{conn.Close();}
conn.Dispose();
tn.Dispose();
}
}
相关文章推荐
- [翻译]如何在C#和ado.net中使用事务
- [原创]C#中使用ADO.NET连接SQL Server数据库,自动增长字段用作主键,处理事务时的基本方法
- [转载]C#中使用ADO.NET连接SQL Server数据库,自动增长字段用作主键,处理事务时的基本方法
- SQLite简单使用(C# ADO.net)
- 利用SQLite ADO.NET在.NET(C#)中使用SQLite数据库
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- C#二十六 使用Ado.Net调用存储过程
- 在C#中使用ADO.Net部件来访问Access数据库
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 在C#中使用ADO.Net部件来访问Access数据库
- HOW TO:使用 ADO.NET 和 Visual C# .NET 调用带参数的存储过程
- sqldatareader返回数据集,并要求使用ADO.net事务的解决方案
- C#二十六 使用Ado.Net调用存储过程
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- ADO.net - 事务使用范例
- 使用ADO.NET 和C# 处理BLOB 数据
- 使用ADO.NET 和C# 处理BLOB 数据
- C#使用ADO.net连接数据库
- C# ado.net 使用 row_number over() 简单的分页示例