博 之 文

以 拼 搏 设 计 梦 想 , 以 恒 心 编 程 明 天
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

/// <summary>        

/// 执行一个SQL语句或者存储过程(无事务控制),返回一个内存表        

/// </summary>        

/// <param name="connectionString">连接串</param>        

/// <param name="commandType">SQL命令的类型</param>        

/// <param name="commandText">SQL命令的内容</param>        

/// <param name="commandParameters">参数类实体</param>   

public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)   

{    

  DataSet objDataSet = new DataSet();    

  SqlConnection objConn = new SqlConnection(connectionString);    

  try    

  {     

    objConn.Open();     

    objDataSet = ExecuteDataSet(objConn, commandType, commandText, commandParameters);    

  }    

  catch (Exception ex)    

  {     

    System.Diagnostics.Debugger.Break();                

    if (!(objConn.State == ConnectionState.Closed))                

    {                    

      objConn.Close();                    

      objConn.Dispose();                    

      objConn = null;                

    }     

      throw ex;    

  }    

  finally    

  {     

    if (!(objConn.State == ConnectionState.Closed))     

    {      

      objConn.Close();      

      objConn.Dispose();      

      objConn = null;     

    }    

  }    

  return objDataSet;   

}

 

 

///ExecuteDataSet函数

/// <summary>        

/// 执行一个SQL语句或者存储过程(无事务控制),返回一个内存表        

/// </summary>        

/// <param name="connection">SQLServer连接类实体</param>        

/// <param name="commandType">SQL命令的类型</param>        

/// <param name="commandText">SQL命令的内容</param>        

/// <param name="commandParameters">参数类实体</param>        

public static DataSet ExecuteDataSet(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)        

{            

  SqlCommand objCommand = new SqlCommand();            

  PrepareCommand(objCommand, connection, null, commandType, commandText, commandParameters);            

  SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);            

  DataSet objDataSet = new DataSet();            

  try            

  {                

    objAdapter.Fill(objDataSet);            

  }            

  catch (Exception ex)            

  {                

    if (!(connection.State == ConnectionState.Closed))                

    {                    

      connection.Close();                    

      connection.Dispose();                    

      connection = null;                

    }                

    System.Diagnostics.Debugger.Break();                

    throw ex;            

  }            

  objCommand.Parameters.Clear();            

  return objDataSet;        

}

 

///PrepareCommand函数

private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)   

{    

  if ((connection.State != ConnectionState.Open))    

  {     

    connection.Open();    

  }    

  command.Connection = connection;    

  command.CommandText = commandText;    

  if ((!(transaction == null)))    

  {     

    command.Transaction = transaction;   

  }    

  command.CommandType = commandType;            

  command.CommandTimeout = 120; //超时改为120秒    

  if ((!(commandParameters == null)))    

  {     

    AttachParameters(command, commandParameters);    

  }   

}  

 

\\\AttachParameters函数

private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)   

{    

  SqlParameter[] parameterArray1 = commandParameters;    

  for (int iNum1 = 0; iNum1 <= parameterArray1.Length - 1; iNum1++)    

  {     

    SqlParameter parameter1 = parameterArray1[iNum1];                

    if (parameter1 == null)                

    {                    

      continue;                

    }     

    if (((parameter1.Direction == ParameterDirection.InputOutput) && (parameter1.Value == null)))     

    {      

      parameter1.Value = DBNull.Value;     

    }     

    command.Parameters.Add(parameter1);    

   }   

}