WEB开发网
开发学院软件开发C语言 c#中事务、批量提交、回滚的写法 阅读

c#中事务、批量提交、回滚的写法

 2009-04-04 08:22:39 来源:WEB开发网   
核心提示:public void UpdateContactTableByDataSet(DataSet ds,string strTblName) { try { SqlDataAdapter myAdapter = new SqlDataAdapter(); SqlConnection conn = new SqlConne

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;
}
}

Tags:事务 批量 提交

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接