DBHelper 助手类

/*
 *创建人:夏楚枫        
 *创建时间:2010-01-31 19:27
 *说明:数据库助手类
 *版权所有夏楚枫[email protected]
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace MyBlogDAL
{

    public class DBHelper
    {
        public static SqlConnection ReturnConn()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["CONN"].ToString());
            if (conn.State.Equals(ConnectionState.Closed))
            {
                conn.Open();
            }
            return conn;
        }
        public static SqlCommand ReturnCmd(string sql, SqlParameter[] prams, SqlConnection conn)
        {
            if (conn.State.Equals(ConnectionState.Closed))
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sql;
            if (prams != null)
            {
                foreach (SqlParameter parameter in prams)
                {
                    if (parameter != null)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                }

            }
            return cmd;
        }
        public static SqlCommand ReturnCmd(string sql, SqlConnection conn)
        {
            if (conn.State.Equals(ConnectionState.Closed))
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sql;
            return cmd;
        }
        public static SqlCommand ReturnCmd(string sql, SqlParameter[] prams)
        {
            SqlConnection conn = ReturnConn();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sql;
            if (prams != null)
            {
                foreach (SqlParameter parameter in prams)
                {
                    if (parameter != null)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                }
            }
            return cmd;
        }
        public static SqlCommand ReturnCmd(string sql)
        {
            SqlConnection conn = ReturnConn();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = sql;
            return cmd;
        }


        public static SqlDataReader ReturnReader(string sql, SqlParameter[] prams, SqlConnection conn)
        {
            SqlCommand cmd = ReturnCmd(sql, prams, conn);
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public static SqlDataReader ReturnReader(string sql, SqlParameter[] prams)
        {
            SqlCommand cmd = ReturnCmd(sql, prams);
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public static SqlDataReader ReturnReader(string sql, SqlConnection conn)
        {
            SqlCommand cmd = ReturnCmd(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public static SqlDataReader ReturnReader(string sql)
        {
            SqlCommand cmd = ReturnCmd(sql);
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public static int ReturnExecuteNonQuery(string sql, SqlParameter[] prams)
        {
            SqlConnection conn = ReturnConn();
            SqlCommand cmd = ReturnCmd(sql, prams, conn);
            int intResult = cmd.ExecuteNonQuery();
            conn.Close();
            return intResult;
        }
        public static int ReturnExecuteNonQuery(string sql)
        {
            SqlConnection conn = ReturnConn();
            SqlCommand cmd = ReturnCmd(sql, conn);
            int intResult = cmd.ExecuteNonQuery();
            conn.Close();
            return intResult;
        }

        public static int ReturnExecuteScalar(string sql)
        {
            SqlConnection conn = ReturnConn();
            SqlCommand cmd = ReturnCmd(sql, conn);
            int intResult = Convert.ToInt32(cmd.ExecuteScalar());
            conn.Close();
            return intResult;
        }

        public static int ReturnExecuteScalar(string sql, SqlParameter[] prams)
        {
            SqlConnection conn = ReturnConn();
            SqlCommand cmd = ReturnCmd(sql, prams, conn);
            int intResult = Convert.ToInt32(cmd.ExecuteScalar());
            conn.Close();
            return intResult;
        }

        public static DataTable ReturnGetTable(string sql, SqlParameter[] prams, SqlConnection conn)
        {
            SqlCommand cmd = ReturnCmd(sql, prams, conn);
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            sda.SelectCommand = cmd;
            sda.Fill(ds);
            DataTable dt = ds.Tables[0];
            conn.Close();
            return dt;
        }
    }
}

你可能感兴趣的:(help)