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

posted @ 2010-03-24 16:37  Me疯子_(~  阅读(226)  评论(0编辑  收藏  举报