C#中读取SQLServer数据库方法类库


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DBUtility
{
    public class SqlHelper
    {
        //通过配置文件(app.config:xml)读取连接字符串
        public static string connectionString = ConfigurationManager .ConnectionStrings["ConnectionString"].ConnectionString;
       
        //字段,通过连接字符串获取连接对象
        private SqlConnection con = new SqlConnection(connectionString);

        //属性,判断连接对象的状态并打开连接对象
        public SqlConnection Con
        {
            get {
                switch (con.State)
                {
                    case ConnectionState.Broken:
                        con.Close(); //先正常关闭,释放资源
                        con.Open();
                        break;
                    case ConnectionState.Closed:
                        con.Open();
                        break;
                    case ConnectionState.Connecting:
                        break;
                    case ConnectionState.Executing:
                        break;
                    case ConnectionState.Fetching:
                        break;
                    case ConnectionState.Open:
                        break;
                    default:
                        break;
                }
                return con; }
            set { con = value; }
        }

        //执行存储过程或者SQL语句并返回数据集DataSet
        public DataSet GetDataSet(string strSQL, CommandType cmdType, params SqlParameter[] values)
        {
            SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

        //执行存储过程或者SQL语句并返回SqlDatareader
        public SqlDataReader GetDataReader(string strSQL, CommandType cmdType, params SqlParameter[] values)
        {
            SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }

        //执行存储过程或者SQL语句并返回首行首列(新增方法的主键)
        public object ExecuteScalar(string strSQL, CommandType cmdType, params SqlParameter[] values)
        {
            SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);
            return cmd.ExecuteScalar();
        }

        //执行存储过程或者SQL语句并返回受影响行数
        public int ExecuteNonQuery(string strSQL, CommandType cmdType, params SqlParameter[] values)
        {
            SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);
            return cmd.ExecuteNonQuery();
        }

        //内部方法,实例化命令对象并配置相关属性
        private SqlCommand PrepareCommand(string strSQL, CommandType cmdType,params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Con;
            cmd.CommandText = strSQL;
            cmd.CommandType = cmdType;
            cmd.CommandTimeout = 60;
            cmd.Parameters.AddRange(values);
            return cmd;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值