DBHelper类

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;
        }
    } 
 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。