DBHelper

public class DBHelper
{
    //属性:数据库链接对象
    private static SqlConnection conn;
    public static SqlConnection Conn
    {
        get
        {
         //   string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; ///////// connstr 更据实际情况更改
            string connstr = ConfigurationSettings.AppSettings["DataBase"]; 
            if (conn == null)
                conn = new SqlConnection(connstr);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            if (conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }
            return conn;
        }
    }


    //方法:查询,DataReader
    public static SqlDataReader GetReader(string SqlStr)
    {
        SqlCommand cmd = new SqlCommand(SqlStr, Conn);
        return cmd.ExecuteReader();
    }

    public static SqlDataReader GetReader(string SqlStr, SqlParameter[] paras)
    {
        SqlCommand cmd = new SqlCommand(SqlStr, Conn);
        cmd.Parameters.AddRange(paras);
        return cmd.ExecuteReader();
    }


    //查询:DataTable
    public static DataTable GetTable(string SqlStr)
    {
        SqlDataAdapter dap = new SqlDataAdapter(SqlStr, Conn);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        conn.Close();
        return ds.Tables[0];

    }

    //增删改
    public static bool Execute(string SqlStr)
    {
        SqlCommand cmd = new SqlCommand(SqlStr, Conn);
        int result = cmd.ExecuteNonQuery();
        conn.Close();
        return result > 0;
    }

    //返回首行首列
    public static object GetScalar(string SqlStr)
    {
        SqlCommand cmd = new SqlCommand(SqlStr, Conn);
        object obj = cmd.ExecuteScalar();
        conn.Close();
        return obj;
    }
}

----------------------------------------------------------
///////////////下面的类执行存储过程
----------------------------------------------------------

 public class execProc
 {
  string ProcName ="";
  SqlParameter[] SqlParameter = null;
  string DBName = "";
  
        /// <summary>
        ///存储过程构造函数
        /// </summary>
        /// <param name="procName">名称</param>
        /// <param name="sqlParameter">参数</param>
        /// <param name="dbName">数据库名</param>
  public execProc(string procName,SqlParameter[] sqlParameter,string dbName)
  {
   this.ProcName = procName;
   this.SqlParameter = sqlParameter;
   this.DBName = dbName;
  }


  /// 构建 SqlCommand 对象
  private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
  {
   SqlCommand command = new SqlCommand(storedProcName, connection);
   command.CommandType = CommandType.StoredProcedure;
   foreach (SqlParameter parameter in parameters)
   {
    if (parameter != null)
    {
     // 检查未分配值的输出参数,将其分配以DBNull.Value.
     if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
      (parameter.Value == null))
     {
      parameter.Value = DBNull.Value;
     }
     command.Parameters.Add(parameter);
    }
   }

   return command;
  }


  /// <summary>
  /// 执行存储过程返回DATATABLE
  /// </summary>
  /// <returns></returns>
  public  DataTable ExecProcReturnDatatable()
  {
  
   SqlConnection con= new SqlConnection();
   SqlDataAdapter da=new SqlDataAdapter();
   DataSet ds=new DataSet();
   DataTable dt = new DataTable();
            con = DBHelper.Conn;  //调用DBHELPER的东西

   SqlCommand command = BuildQueryCommand(con, ProcName, SqlParameter);
   //建立数据池
   da = new SqlDataAdapter(command);
   try
   {
    command.ExecuteNonQuery();
    ds = new DataSet();
    da.Fill(ds);
    dt = ds.Tables[0];
    int i = dt.Rows.Count;
   }
   catch(Exception ex)
   {
    return null;
   }
   finally
   {
    con.Close();
    command.Dispose();
    con.Dispose();
   }
   
   return dt;

  }
 }

 

你可能感兴趣的:(help)