c#中事务、批量提交、回滚的写法
2009-04-04 08:22:39 来源:WEB开发网public void UpdateContactTableByDataSet(DataSet ds,string strTblName)
{
try
{
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand myCommand = new SqlCommand("select * from strTblName",conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
conn.Open();
SqlTransaction myTrans = conn.BeginTransaction();
myCommand.Transaction = myTrans;
try
{
myAdapter.Update(ds,strTblName);
myTrans.Commit();
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("回滚失败! 异常类型: " + ex.GetType());
}
}
}
finally
{
conn.Close();
}
}
catch(Exception ex)
{
throw ex;
}
}
更多精彩
赞助商链接