`
cbting
  • 浏览: 58080 次
  • 性别: Icon_minigender_1
  • 来自: 汕头
社区版块
存档分类
最新评论

SQLHelper类

    博客分类:
  • C#
阅读更多

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 SQLhepler
    {
        private SqlConnection connection = null;
        private SqlCommand comm = null;
        private SqlDataReader der = null;
        public SQLhepler()
        {
            string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
            connection = new SqlConnection(connstr);
        }
        private SqlConnection getconn()
        {
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
            return connection;
        }
        /// <summary>
        /// 执行不带参数的sql增删改语句或存储过程
        /// </summary>
        /// <param name="cmdtext">增删改sql或存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public int ExecuteNonQuery(string cmdtext, CommandType ct)
        {
            int ints;
            try
            {
                comm = new SqlCommand(cmdtext, getconn());
                comm.CommandType = ct;
                ints = comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                }
            }
            return ints;
        }
        /// <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();
            comm = new SqlCommand(cmdtext, getconn());
            comm.CommandType = ct;
            using (der = comm.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(der);
            }
            return dt;
        }
        /// <summary>
        /// 执行带参数的sql增删改语句或存储过程
        /// </summary>
        /// <param name="cmdtext">sql增删改语句或存储过程</param>
        /// <param name="para">参数集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public int ExecuteNonQuery(string cmdtext, SqlParameter[] para, CommandType ct)
        {
            int ints;
            using (comm = new SqlCommand(cmdtext, getconn()))
            {
                comm.CommandType = ct;
                comm.Parameters.AddRange(para);
                ints = comm.ExecuteNonQuery();
            }

            return ints;
        }
        /// <summary>
        /// 执行带参数的sql查询语句或存储过程;
        /// </summary>
        /// <param name="cmdtext"></param>
        /// <param name="para"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public DataTable ExecuteQuery(string cmdtext, SqlParameter[] para, CommandType ct)
        {
            DataTable dt = new DataTable();
            comm = new SqlCommand(cmdtext, getconn());
            comm.CommandType = ct;
            comm.Parameters.AddRange(para);
            using (der = comm.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(der);
            }
            return dt;
        }
    }
}

分享到:
评论

相关推荐

    Sqlhelper类的的内容

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

    C# SqlHelper类 (微软官方)

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

    最新强大而简便的SqlHelper类

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

    微软的SQLHelper类 (含完整中文注释) c# asp.net winform

    微软的SQLHelper类 (含完整中文注释) c# asp.net winform 微软的SQLHelper类是微软提供的一个数据访问帮助类,主要用于执行SQL Server数据库操作。该类提供了多种方法来执行SQL语句、存储过程和Scalar查询。下面是...

    非常实用的SqlHelper类

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

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

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

    SqlHelper类

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

    SQLHelper类的使用

    教你.net中SQLHelper类的使用

    SQLHelper类(Java版)

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

    微软官方sqlhelper类

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

    微软官方的SQLHelper类(含完整中文注释)

    微软官方的SQLHelper类(含完整中文注释)

    sqlhelper类

    sqlhelper类 供大家使用。。。。。。。。。。。。。。

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

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

    微软SqlHelper类 中文版 中的方法及说明整理

    微软SqlHelper类 中文版 中的方法及说明整理

    sqlhelper类,sqlhelper类,

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

    sqlhelper类详解

    sqlhelper类详细解说,简单明了,适合于.net新手。

Global site tag (gtag.js) - Google Analytics