将List集合插入到数据库中,C#中的事物实现

SqlConnection conn  =   new  SqlConnection();
        conn.Open();
        SqlTransaction tran 
=  conn.BeginTransaction();

        
try
        {
            
foreach ()
            {
                
string  sql = "" ;
                SqlCommand cmd
= new  SqlCommand(sql,conn,tran);

                SqlParameter[] pars
= new  SqlParameter[]{
                    
new  SqlParameter( "" ,value),
                    ...
                }

                cmd.Parameters.AddRange(pars);
                cmd.ExecuteNonQuery();                    
            }
            tran.Commit();
        }
catch
        {
            tran.Rollback();
        }
        
finally
        {
            conn.Close();
        }

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