日期:2014-05-18 浏览次数:20830 次
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; /// <summary> ///创建时间:2008-8-4 /// 创 建 人:吕豪 /// 作 用:用于数据库的链接, /// </summary> public class DataBase { /// <summary> /// 保护变量,数据库连接 /// </summary> protected SqlConnection Connection; /// <summary> /// 保护变量,数据库链接串 /// </summary> protected string ConnectionString; /// <summary> /// 构造函数 /// </summary> /// <spram name=ConnectionString>数据库连接字符串</spram> public DataBase() { ConnectionString = ConfigurationManager.AppSettings["SqlCoon"]; } /// <summary> /// 析构函数。释放非托管资源 /// </summary> ~DataBase() { try { if (Connection != null) Connection.Close(); } catch { } try { Dispose(); } catch { } } /// <summary> /// 保护方法,打开数据库连接 /// </summary> protected void open() { if (Connection == null) { Connection = new SqlConnection(ConnectionString); } if (Connection.State.Equals(ConnectionState.Closed)) { Connection.Open(); } } /// <summary> /// 公有方法,关闭数据库连接 /// </summary> public void Close() { if (Connection != null) { Connection.Close(); } } /// <summary> /// 公有方法。释放资源 /// </summary> public void Dispose() { //确保数据库被关闭 if (Connection != null) { Connection.Dispose(); Connection = null; } } /// <summary> /// 共有方法,获取数据,返回一个SqldataReader() /// </summary> /// <param name="SqlString">Sql语句</param> /// <returns>SqlDataReader</returns> public SqlDataReader GetSaDataReader(string SqlString) { open(); SqlCommand cmd = new SqlCommand(SqlString, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; reader.Close(); Connection.Close(); } /// <summary> /// 共有方法,返回DataSet /// </summary> /// <param name="SqlString"></param> /// <returns></returns> public DataSet GetDataSet(string SqlString) { open(); SqlDataAdapter adapter = new SqlDataAdapter(SqlString, Connection); DataSet dataset = new DataSet(); adapter.Fill(dataset); Close(); adapter.Dispose(); dataset.Dispose(); return dataset; } /// <summary> /// 共有方法,执行增,删,改 /// </summary> /// <param name="SqlString">Sql语句</param> /// <returns>true Or false</returns> public bool ExecuteInfo(string SqlString) { open(); SqlCommand cmd = new SqlCommand(SqlString, Connection); cmd.ExecuteNonQuery(); return true; } /// <summary> /// 执行统计信息 /// </summary> /// <param name="SqlString"></param> /// <returns></returns> public int CountInfo(string SqlString) { open(); SqlCommand cmd = new SqlCommand(SqlString, Connection); int i =Convert.ToInt32(cmd.ExecuteScalar()); return i; } }