使用SqlTransaction创立数据库连接

使用SqlTransaction创立数据库连接

一、连接字符串 

public static readonly string connString = System.Configuration.ConfigurationSettings.AppSettings["connString"];
 

二、一个封装了ExecuteNonQuery()操作的方法,使用SqlTransaction建立数据库连接.   
  public static int ExecuteNonQuery(SqlTransaction transaction, string cmdText, SqlParameter[] parms,CommandType cmdtype)
  {
   int result= 0;

   SqlCommand cmd = new SqlCommand(cmdText);
   cmd.Connection = tran.Connection;
   cmd.Transaction = transaction;
   cmd.CommandType = cmdtype;
   cmd.CommandTimeout = 100;

   if ( parms != null)
   {
    foreach (SqlParameter parm in parms)
    {
     cmd.Parameters.Add(parm);
    }
   }
   
   result = cmd.ExecuteNonQuery(); 
   cmd.Parameters .Clear ();

   return result;
  }

 

 

三、另外一个封装了ExecuteNonQuery()操作的数据库操作方法,使用SqlTransaction连接数据库

public static int ExecuteNonQuery(SqlTransaction transaction, string cmdText)
  {
   int result = 0;

   SqlCommand cmd = new SqlCommand(cmdText);
   cmd.Connection = tran.Connection;
   cmd.Transaction = transaction;
   cmd.CommandTimeout = 100;
   
   result= cmd.ExecuteNonQuery(); 

   return result;
  }

你可能感兴趣的:(transaction)