首页 > 代码库 > DBHelper类

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

 

DBHelper类