• 重构的数据库助手类


    /*
     * 创建人:张良伟
     * 创建时间:2013-12-10 19:00
     * 说明:数据库助手类
     * 版权所有:张良伟&www.tg029.com(众志网)
     */
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace DAL
    {
        public class SQLHelper
        {
            private SqlConnection conn = null;
            private SqlCommand cmd = null;
            private SqlDataReader sdr = null;
            public SQLHelper()
            {
                string connStr = @"server=2013-20131109GT;database=newssystem;uid=sa;pwd=123456";
                conn = new SqlConnection(connStr);
            }

            private SqlConnection GetConn()
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                return conn;
            }

            /// <summary>
            ///  执行不带参数的增删改SQL语句或存储过程
            /// </summary>
            /// <param name="cmdText">增删改SQL语句或存储过程</param>
            /// <param name="ct">命令类型</param>
            /// <returns></returns>
            public int ExecuteNonQuery(string cmdText, CommandType ct)
            {
                int res;
                try
                {
                    cmd = new SqlCommand(cmdText, GetConn());
                    cmd.CommandType = ct;
                    res = cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
                return res;
            }

            /// <summary>
            ///  执行带参数的增删改SQL语句或存储过程
            /// </summary>
            /// <param name="cmdText">增删改SQL语句或存储过程</param>
            /// <param name="ct">命令类型</param>
            /// <returns></returns>
            public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
            {
                int res;
                using (cmd = new SqlCommand(cmdText, GetConn()))
                {
                    cmd.CommandType = ct;
                    cmd.Parameters.AddRange(paras);
                    res = cmd.ExecuteNonQuery();
                }
                return res;
            }

            /// <summary>
            ///  执行查询SQL语句或存储过程
            /// </summary>
            /// <param name="cmdText">查询SQL语句或存储过程</param>
            /// <param name="ct">命令类型</param>
            /// <returns></returns>
            public DataTable ExecuteQuery(string cmdText, CommandType ct)
            {
                DataTable dt = new DataTable();
                cmd = new SqlCommand(cmdText, GetConn());
                cmd.CommandType = ct;
                using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    dt.Load(sdr);
                }
                return dt;
            }

            /// <summary>
            ///  执行带参数的查询SQL语句或存储过程
            /// </summary>
            /// <param name="cmdText">查询SQL语句或存储过程</param>
            /// <param name="paras">参数集合</param>
            /// <param name="ct">命令类型</param>
            /// <returns></returns>
            public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
            {
                DataTable dt = new DataTable();
                cmd = new SqlCommand(cmdText, GetConn());
                cmd.CommandType = ct;
                cmd.Parameters.AddRange(paras);
                using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    dt.Load(sdr);
                }
                return dt;
            }
        }
    }

  • 相关阅读:
    发现一个奇怪的问题: 不能把文件取名为 con
    博客园新购服务器硬件配置
    [重要新功能]团队Blog
    [庆祝]博客园已迁至新服务器
    [公告]博客园论坛开放注册
    博客园出现了奇怪的cookie问题
    [公告]博客园聊天室试运行
    [重发]请为你喜欢的博客园杂志的名字投上一票
    [征询意见]准备采用“创作共用”协议保护大家的原创作品
    博客园期刊制作小组Blog开通
  • 原文地址:https://www.cnblogs.com/liangwei/p/3468325.html
Copyright © 2020-2023  润新知