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.Collections;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Data.OleDb;
/// <summary>
/// DBConnection 的摘要说明
/// </summary>
public class DBConnection
{
protected static string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("/aspnet/App_Data/student.mdb");//ACCESS链接字符串
public static OleDbConnection conn = new OleDbConnection(strConn);//OleDb链接类的实例化
public DBConnection()
{
}
public static OleDbConnection Db_Conn()
{
if (conn.State != ConnectionState.Open)
{ conn.Open(); }
return conn;
}
public static OleDbDataReader ExecuteReader(string CommandTxt) //ExecuteReader
{
try
{
OleDbCommand Commd = new OleDbCommand(CommandTxt, conn);
Commd.CommandType= CommandType.Text;
OleDbDataReader Dr;
Db_Conn();
Dr = Commd.ExecuteReader();
Commd.Dispose();
return Dr;
}
catch (System.Data.SqlClient.SqlException e)
{ conn.Close(); throw new Exception(e.Message); }
finally { }
}
public void ExecuteNonQuery(string CommandTxt)
{
try
{
using (OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn))
{
Db_Conn();
OleDbCommd.ExecuteNonQuery();
OleDbCommd.Dispose();
}
}
catch (Exception ex)
{ throw ex; }
finally
{ conn.Close(); }
}
public static void DataBind(System.Web.UI.WebControls.GridView MYGridView, string CommandTxt)
{
try
{
OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn);
OleDbCommd.CommandType = CommandType.Text;
OleDbDataReader Dr;
Db_Conn();
Dr = OleDbCommd.ExecuteReader();
MYGridView.DataSource = Dr;
MYGridView.DataBind();
Dr.Close();
OleDbCommd.Dispose();
close();
}
catch (Exception ex)
{ close(); throw ex; }
}
public static void DataBind(System.Web.UI.WebControls.DataList MyDataList, string CommandTxt)
{
try
{
OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn);
OleDbCommd.CommandType = CommandType.Text;
OleDbDataReader Dr;
Db_Conn();
Dr = OleDbCommd.ExecuteReader();
MyDataList.DataSource = Dr;
MyDataList.DataBind();
Dr.Close();
OleDbCommd.Dispose();
close();
}
catch (Exception ex)
{ close(); throw ex; }
}
public static void DataBind(System.Web.UI.WebControls.Repeater MyRepeater, string CommandTxt)
{
try
{
OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn);
OleDbCommd.CommandType = CommandType.Text;
OleDbDataReader Dr;
Db_Conn();
Dr = OleDbCommd.ExecuteReader();
MyRepeater.DataSource = Dr;
MyRepeater.DataBind();
Dr.Close();
OleDbCommd.Dispose();
close();
}
catch (Exception ex)
{ close(); throw ex; }
}
public static DataSet FillDataSet(string sql)
{
DataSet ds = new DataSet();
try
{
Db_Conn();
using (OleDbCommand Commd = new OleDbCommand(sql, conn))
{
OleDbDataAdapter adapter = new OleDbDataAdapter(Commd);
adapter.Fill(ds);
return ds;
}
}
catch (Exception Ex)
{ throw Ex; }
finally
{ close(); }
}
public static int GetRowsNum(string sqlstr) //判断几个记录
{
DataSet ds = FillDataSet(sqlstr);
return ds.Tables[0].Rows.Count;
}
public static bool IfRead(string CommandTxt)
{
try
{
if (GetRowsNum(CommandTxt) == 0)
{ return false; }
else
{ return true; }
}
catch (Exception Ex)
{ throw Ex; }
finally { }
}
public static void close()
{
if (conn.State != ConnectionState.Closed)
{ conn.Close(); }
}
}