C#数据回滚实例

private void save_db(){

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[""].ToString());

SqlCommand comm = new SqlCommand();

conn.Open();

SqlTransaction rollbk2= conn.BeginTransaction();

comm.Connection=conn;

comm.Transaction=rollbk2;

try

{

for(i){

comm.CommandText="insert into ";

comm.ExecuteNonQuery();

}

rollbk2.Commit();

}

catch(Exception exp)

{

rollbk2.Rollback();

Response.Write("save_fail");

return;

}

finally

{

conn.Close();

}

Response.Write("save_ok");

}

你可能感兴趣的:(C#)