`
白玉水堂
  • 浏览: 14602 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

SQLHelper助手类

 
阅读更多
/*
* 创建时间:2013-10-12 14:52
* 说明:数据库助手类
*/
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 = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            conn = new SqlConnection(connStr);
        }

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

        #region 执行不带参数的SQL增删改语句或存储过程
        /// <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;
        }
        #endregion

        #region 执行带参数的SQL增删改语句或存储过程
        /// <summary>
        /// 执行带参数的SQL增删改语句或存储过程
        /// </summary>
        /// <param name="cmdText">SQL增删改语句或存储过程</param>
        /// <param name="paras">参数集合</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;
        }
        #endregion

        #region 执行不带参数的SQL查询语句或存储过程
        /// <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;
        }
        #endregion

        #region 执行带参数的查询SQL语句或存储过程
         /// <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;
        }
        #endregion

        #region 返回执行不带参数的SQL语句的第一行第一列的值
        /// <summary>
        /// 返回执行不带参数的SQL语句的第一行第一列的值
        /// </summary>
        /// <param name="cmdText">SQL语句</param>
        /// <returns></returns>
        public string ExecuteScalar(string cmdText)
        {
            try
            {
                cmd = new SqlCommand(cmdText, GetConn());
                object obj = cmd.ExecuteScalar();
                if (obj!=null)
                {
                    return obj.ToString();
                }
                return "";
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        #endregion

    }
}
分享到:
评论

相关推荐

    Sqlhelper类的的内容

    Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容Sqlhelper类的的内容

    韩顺平sqlhelper工具类

    韩顺平sqlhelper工具类,一直贯穿于java jsp servlet javaweb中

    C# SqlHelper类 (微软官方)

    微软官方C# SqlHelper类 ,内带有注释说明,供大家参考

    SqlHelper类代码

    这是一个用C#写好的Sqlhelper助手类的代码,希望这个助手类能帮助有需要的人。

    SqlHelper数据库助手类

    链接数据库

    sqlhelper类,sqlhelper类,

    sqlhelper类,可以在做项目时参考,这个类里没有什么错误,适合.net+SQL

    最新强大而简便的SqlHelper类

    2,所以数据库操作只需要调用SQLHelper中的方法即可.DataAccess类是在SQLHelper类内部调用的类. 3,使用前请先执行以下方法,以进行类初始化: string DBConnectionString = "data source=.;database=Northwind;user id...

    SqlHelper.cs 微软的SQLHelper类(含完整中文注释)

    SqlHelper.cs 微软的SQLHelper类 微软的SQLHelper类(含完整中文注释) 非常好的SQLHelper

    完整SqlHelper类详细中文注释

    完整SqlHelper类详细中文注释

    SQLHelper类的使用

    教你.net中SQLHelper类的使用

    非常实用的SqlHelper类

    本SqlHelper类(在ZDevTools.Data命名空间中)最与众不同的地方在于兼顾了易用性与灵活性:她通过委托的方式将你要执行的操作传入SqlHelper类中,所有的连接操作自动维护,却能将DBDataReader、DBCommand这样功能...

    jdbc中操作数据库助手Sqlhelper

    jdbc中操作数据库助手Sqlhelper

    SqlHelper通用类

    SqlHelper 操作数据库通用类 ado.net asp.net 有详细的说明 十分好用

    SqlHelper帮助类

    自己写的ADO.Net的SqlHelper帮助类,每个方法的类型都有,自己用着很方便

    SqlHelper类

    SqlHelper类。内置了常用的操作数据库的方法,非常实用的类。平时开发就可以省略好多SQL语句。注:C#写的

    微软官方sqlhelper类

    微软官方的sqlhelper类,包含访问sqlserver和access的类,可以编译作为类库引用,也可以直接以代码形式加入项目,对于直接使用ADO.net访问数据库的小型项目来说,非常适用,大大减少了访问数据库的代码量。...

    精简的SqlHelper帮助类,含注释说明

    该Sqlhelper.cs帮助类中包含基本的数据库方法:ExecuteNonQuery、ExecuteReader、ExecuteScalar;代码总共才266行...给需要的孩童们...

    SQLHelper类(Java版)

    SQLHelper类(Java版), 模仿微软提供的SQLHelper类的功能,真正实现了除SQLHelper类外,外界全部可以无须引用连接数据库的类,并且无须担心数据库的连接与关闭。(内附使用说明)

    C#公共类SQLHelper

    详细的SQLhelper类!使用在数据链路层减少了大量的代码工作!

    sqlhelper类

    sqlhelper 数据库连接的一个关键类

Global site tag (gtag.js) - Google Analytics