ADO.NET数据库访问通用类(SQLHelper第一版)

///
///模块编号:1001
///作用:数据库访问通用类
///作者:邓政军
///编写日期:2011-11-30
///

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SqlHelper
{
    public class SQLHelper
    {
        /// 
        /// 获取数据连接字符串
        /// 
        /// 连接字符串
        public static string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["ASPNET3_5_DataBase"].ConnectionString;
        }

        /// 
        /// 执行查询并将结果返回至DataTable中
        /// 
        /// 查询语句
        /// 可能带的参数
        /// 返回一张查询结果表
        public static DataTable ExecuteDataTable(string strSql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    foreach (SqlParameter p in parameters)
                    {
                        cmd.Parameters.Add(p);
                    }
                    DataSet ds = new DataSet();
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        adapter.Fill(ds);
                        return ds.Tables[0];
                    }
                }
            }
        }

        /// 
        /// (重载)执行查询并将结果返回至DataTable中
        /// 
        /// 查询语句
        /// 返回一张查询结果表
        public static DataTable ExecuteDataTable(string strSql)
        {
            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    DataSet ds = new DataSet();
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        adapter.Fill(ds);
                        return ds.Tables[0];
                    }
                }
            }
        }


        /// 
        /// 执行对数据的增删改操作
        /// 
        /// 
        /// 
        public static void ExecuteNonQuery(string strSql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    foreach (SqlParameter p in parameters)
                    {
                        cmd.Parameters.Add(p);
                    }
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// 
        /// (重载)执行对数据的增删改操作
        /// 
        /// 查询语句
        public static void ExecuteNonQuery(string strSql)
        {
            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// 
        /// 执行查询并返回结果集中第一行第一列的值
        /// 
        /// 
        /// 
        /// 
        public static object ExecuteScalar(string strSql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    foreach (SqlParameter p in parameters)
                    {
                        cmd.Parameters.Add(p);
                    }
                    return cmd.ExecuteScalar();
                }
            }
        }

        /// 
        /// (重载)执行查询并返回结果集中第一行第一列的值
        /// 
        /// 查询语句
        /// 
        public static object ExecuteScalar(string strSql)
        {
            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    return cmd.ExecuteScalar();
                }
            }
        }

    }
}

你可能感兴趣的:(ADO.NET)