当前位置导航:炫浪网>>网络学院>>编程开发>>Visual C#教程

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

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

相关内容
赞助商链接