快捷搜索:

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;

}

}

您可能还会对下面的文章感兴趣: