DBHelper类使用

1.先在Web.config 文件中加入要连接的数据库

里面加入

    
      
    

name:是自己起的名字

connectionString:连接数据库字段

2.编写DBHelper类

先引入命名空间:using System.Configuration;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
namespace HP_DAL
{
    public class DBHelper
    {
        /// 
        /// 连接字符串
        /// 
       private readonly static string config = ConfigurationManager.ConnectionStrings["logisDb"].ConnectionString;
        /// 
        /// 离线查询,返回DataTable
        /// 
        /// 
        /// 
        /// 
        public static DataTable ExecuteTable(string sql,params SqlParameter[] par)
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, config))
            {
                if (par != null && par.Length > 0)
                {
                    sda.SelectCommand.Parameters.AddRange(par);
                }
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }

        }
        /// 
        /// 查询首行首列,返回object
        /// 
        /// 
        /// 
        /// 
        public static object ExecuteScalar(string sql, params SqlParameter[] par)
        {
            using (SqlConnection con = new SqlConnection(config))
            {
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    if (par != null && par.Length > 0)
                    {
                        com.Parameters.AddRange(par);
                    }
                    if (con.State!=ConnectionState.Open)
                    {
                        con.Open();
                    }
                    return com.ExecuteScalar();
                }
            }
        }
        /// 
        /// 在线查询,返回SqlDataReader,存储过程
        /// 
        /// 
        /// 
        /// 
        public static SqlDataReader ExecuteReader1(string procname, params SqlParameter[] par)
        {
            SqlConnection con = new SqlConnection(config);

            using (SqlCommand com = new SqlCommand(procname, con))
            {
                com.CommandType = CommandType.StoredProcedure;
                if (par != null && par.Length > 0)
                {
                    com.Parameters.AddRange(par);
                }
                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                }
                return com.ExecuteReader(CommandBehavior.CloseConnection);

            }
        }
        /// 
        /// 在线查询,返回SqlDataReader
        /// 
        /// 
        /// 
        /// 
        public static SqlDataReader ExecuteReader(string procname, params SqlParameter[] par)
        {
            SqlConnection con = new SqlConnection(config);
            
                using (SqlCommand com = new SqlCommand(procname, con))
                {
             
                    if (par != null && par.Length > 0)
                    {
                        com.Parameters.AddRange(par);
                    }
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
                    return com.ExecuteReader(CommandBehavior.CloseConnection);
                
            }
        }
        /// 
        /// 增删改方法
        /// 
        /// 
        public static int ExecuteNonQuery(string sql, params SqlParameter[] par)
        {
            using (SqlConnection con = new SqlConnection(config))
            {
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    if (par != null && par.Length > 0)
                    {
                        com.Parameters.AddRange(par);
                    }
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
                    return com.ExecuteNonQuery();
                }
            }
        }
        /// 
        /// 增删改方法,存储过程
        /// 
        /// 
        /// 
        /// 
        public static int ExecuteNonQueryProc(string sql, params SqlParameter[] par)
        {
            using (SqlConnection con = new SqlConnection(config))
            {
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    com.CommandTimeout = 60;
                    com.CommandType = CommandType.StoredProcedure;
                    if (par != null && par.Length > 0)
                    {
                        com.Parameters.AddRange(par);
                    }
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
                    return com.ExecuteNonQuery();
                }
            }
        }
    }
}

 

你可能感兴趣的:(ado.net,十一的博客,DBHelper类使用,C#DBHelper类,dbhelper)