码迷,mamicode.com
首页 > 数据库 > 详细

DBHelper类

时间:2014-08-29 17:45:48      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   使用   io   ar   文件   div   

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Business.DAL
 { 
    public static class DBHelper
    {
         //以下被注的是连接配置文件用,使用时可先编写配置文件,把Name设置为ConnectionString
         private static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
         //private static string connectionString = "server=WYP;database=MXDB1;uid=sa;pwd=sa";
         private static SqlConnection connection;
         public static SqlConnection Connection
         {
             get
             {
                if (connection == null)
                {
                   connection = new SqlConnection(connectionString);
                   connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)
                     {
                        connection.Open();
                     }
                else if (connection.State == System.Data.ConnectionState.Broken)
                     {
                         connection.Close();
                         connection.Open();
                     }
                return connection;
             }
         }
     public static int ExecuteCommand(string safeSql)
     {
        SqlCommand cmd = new SqlCommand(safeSql, Connection);
        int result = cmd.ExecuteNonQuery();
        return result;
     }
     public static int ExecuteCommand(string sql, SqlParameter[] values)
     {
        SqlCommand cmd = new SqlCommand(sql, Connection);
        cmd.Parameters.AddRange(values);
        return cmd.ExecuteNonQuery();
     }
     public static int ExecuteCommand(string sql, SqlParameter value)
     {
         SqlCommand cmd = new SqlCommand(sql, Connection);
         cmd.Parameters.Add(value);
         int result = cmd.ExecuteNonQuery();
         return result;
      }
      public static int ExecuteScalar(string safeSql)
      {
         SqlCommand cmd = new SqlCommand(safeSql, Connection);
         int result = (int)cmd.ExecuteScalar();
         return result;
      } 
       public static int ExecuteScalar(string sql, SqlParameter[] values)
       {
          SqlCommand cmd = new SqlCommand(sql, Connection);
          cmd.Parameters.AddRange(values);
          int result = (int)cmd.ExecuteScalar();
          return result;
       } 
       public static int ExecuteScalar(string sql, SqlParameter value)
       {
          SqlCommand cmd = new SqlCommand(sql, Connection);
          cmd.Parameters.Add(value);
          int result = (int)cmd.ExecuteScalar();
          return result;
       }
       public static SqlDataReader ExecuteReader(string safeSql)
       {
          SqlCommand cmd = new SqlCommand(safeSql, Connection);
          SqlDataReader reader = cmd.ExecuteReader();
          return reader;
       } 
       public static SqlDataReader ExecuteReader(string sql, SqlParameter value)
       {
          SqlCommand cmd = new SqlCommand(sql, Connection);
          cmd.Parameters.Add(value);
          SqlDataReader reader = cmd.ExecuteReader();
          return reader;
       }
       public static SqlDataReader ExecuteReader(string sql, SqlParameter[] values)
       {
          SqlCommand cmd = new SqlCommand(sql, Connection);
          cmd.Parameters.AddRange(values);
          SqlDataReader reader = cmd.ExecuteReader();
          return reader;
       }
       public static DataTable GetDataSet(string safeSql)
        {
           DataSet ds = new DataSet();
           SqlCommand cmd = new SqlCommand(safeSql, Connection);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(ds,"temp");
           return ds.Tables[0];
        }
        public static DataTable GetDataSet(string safeSql,SqlParameter[] values)
        {
           DataSet ds = new DataSet();
           SqlCommand cmd = new SqlCommand(safeSql, Connection);
           cmd.Parameters.AddRange(values);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(ds,"temp");
           return ds.Tables[0];
        }
        public static SqlDataReader GetReader(string safeSql)
        {
           SqlCommand cmd = new SqlCommand(safeSql, Connection);
           SqlDataReader reader = cmd.ExecuteReader();
           return reader;
        }
        public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
        {
           SqlCommand cmd = new SqlCommand(sql, Connection);
           cmd.Parameters.AddRange(values);
           SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
    } 
 }

 

DBHelper类

标签:style   blog   color   os   使用   io   ar   文件   div   

原文地址:http://www.cnblogs.com/hesheng/p/3945231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!