酒店客房管理系统之系统开发--DBHelper类

话不多说,直接上代码!!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace RoomManagementSystem.DAL
{
    public class DBHelper
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        public static string ConnStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();

        #region 执行工具
        /// <summary>
        /// 执行工具
        /// </summary>
        /// <param name="con">连接对象</param>
        /// <param name="cmd">工具</param>
        /// <param name="paras">参数数组</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="cmdType">命令类型</param>
        public static void PrepareCommand(SqlConnection con, SqlCommand cmd, SqlParameter[] paras, string sql, CommandType cmdType)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            cmd.Connection = con;
            cmd.CommandType = cmdType;
            cmd.CommandText = sql;
            if (paras == null)
            {
                return;
            }
            else
            {
                foreach (SqlParameter p in paras)
                {
                    cmd.Parameters.Add(p);
                }
            }
        }
        #endregion

        #region 执行添加、删除、修改的方法
        /// <summary>
        /// 执行添加、删除、修改的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="paras"></param>
        /// <returns>受影响行数</returns>
        public static int ExecuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] paras)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                PrepareCommand(conn, cmd, paras, sql, cmdType);
                int result = cmd.ExecuteNonQuery();
                return result;
            }
        }
        #endregion

        #region 查询首行首列
        /// <summary>
        /// 查询首行首列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] paras)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                PrepareCommand(conn, cmd, paras, sql, cmdType);
                return cmd.ExecuteScalar();
            }
        } 
        #endregion

        #region 执行SQL语句,返回SqlDataReader对象
        /// <summary>
        /// 执行SQL语句,返回SqlDataReader对象
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static SqlDataReader GetDataReader(string sql, CommandType cmdType, params SqlParameter[] paras)
        {
            SqlCommand cmd = new SqlCommand();
            try
            {
                using (SqlConnection conn = new SqlConnection(ConnStr))
                {
                    PrepareCommand(conn, cmd, paras, sql, cmdType);
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    cmd.Parameters.Clear();
                    return reader;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region 获取datatable
        /// <summary>
        /// 获取datatable 
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="paras">参数</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql, CommandType cmdType, params SqlParameter[] paras)
        {
            SqlCommand cmd = new SqlCommand();
            DataTable dt = null;
            try
            {
                using (SqlConnection con = new SqlConnection(ConnStr))
                {
                    PrepareCommand(con, cmd, paras, sql, cmdType);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    dt = new DataTable();
                    adapter.Fill(dt);
                }
                return dt;
            }
            catch (Exception e)
            {

                throw e;
            }
        } 
        #endregion

    }
}

如有问题,请多指教,谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值