using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Access
{
/// <summary>
/// Access数据库连接类。
/// </summary>
public class AccessDB
{
#region 变量生明处
public OleDbConnection Conn;
public OleDbCommand Comm;
public OleDbDataAdapter OleDbAdapter;
public DataSet ds;
public OleDbDataReader dr;
#endregion
#region 数据库连接类生成处
public AccessDB(string DbPath)
{
//
// TODO: 在此处添加构造函数逻辑
//
Conn=new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["DBLink"]+DbPath);
Conn.Open();
}
~AccessDB()
{
if (ds!=null) // 清除DataSet对象
{
ds.Clear();
}
if (Conn!=null)
{
Conn.Close(); // 关闭数据库
}
}
/// <summary>
/// 释放系统资源 断开与数据库的连接
/// </summary>
public void Dispose()
{
if (ds!=null) // 清除DataSet对象
{
ds.Clear();
}
if (Conn!=null)
{
Conn.Close(); // 关闭数据库
}
}
#endregion
#region 基本数据库操作
/// <summary>
/// 返回第一行第一列的值
/// </summary>
/// <param name="str">Sql命令</param>
/// <returns></returns>
public object ExecuteScalar(string str)
{
Comm=new OleDbCommand(str,Conn);
object R= Comm.ExecuteScalar();
Comm.Dispose();
return R;
}
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="str">SQL命令</param>
/// <returns>返回影响行数</returns>
public int Execute(string str)
{
Comm= new OleDbCommand(str,Conn);
int R= Comm.ExecuteNonQuery();
Comm.Dispose();
return R;
}
/// <summary>
/// 运行SQL语句 并返回表
/// </summary>
/// <param name="str">SQL语句</param>
/// <returns></returns>
public OleDbDataReader RunAs(string str)
{
Comm=new OleDbCommand(str,Conn);
return Comm.ExecuteReader();
}
/// <summary>
/// 建立DataSet对象,用记录填充或构架(如果必要)DataSet对象,DataSet即是数据在内存的缓存
/// </summary>
/// <param name="str">打开表Sql语句</param>
public void Fill(string str)
{
OleDbAdapter = new OleDbDataAdapter(str,Conn);
ds = new DataSet();
OleDbAdapter.Fill(ds);
}
#endregion
#region DataReader 运行处
/*------------控件绑定-------------*/
/// <summary>
/// 绑定DataGrid控件并显示数据
/// </summary>
/// <param name="str_Sql">Select-SQL语句</param>
/// <param name="mydatagrid">DataGrid控件id值</param>
public void BindDataGrid(string str,DataGrid MyDataGrid)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
MyDataGrid.DataSource= dr;
MyDataGrid.DataBind();
dr.Close();
}
public void BindDataList(string str,DataList myDataList)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
myDataList.DataSource= dr;
myDataList.DataBind();
dr.Close();
}
/// <summary>
/// 将SQL返回值显示到Repeater中去
/// </summary>
/// <param name="str">Sql命令</param>
/// <param name="myRepeater">Repeater</param>
public void BindRepeater(string str,Repeater myRepeater)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
myRepeater.DataSource=dr;
myRepeater.DataBind();
dr.Close();
}
public void BindDropDownList(string str,DropDownList myDropDownList,string valueField,string textField)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
myDropDownList.DataSource=dr;
myDropDownList.DataValueField=valueField;
myDropDownList.DataTextField=textField;
myDropDownList.DataBind();
dr.Close();
}
public void BindListBox(string str,ListBox myList,string valueField,string TextField)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
myList.DataSource=dr;
myList.DataValueField=valueField;
myList.DataTextField=TextField;
myList.DataBind();
dr.Close();
}
/// <summary>
/// 只有值和显示的值来自数据库
/// </summary>
/// <param name="str"></param>
/// <param name="myCbl"></param>
/// <param name="valueField"></param>
/// <param name="TextField"></param>
public void BindCheckBoxList(string str,CheckBoxList myCbl,string valueField,string TextField)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
myCbl.DataSource=dr;
myCbl.DataValueField=valueField;
myCbl.DataTextField=TextField;
myCbl.DataBind();
dr.Close();
}
public void BindRadioButtonList(string str,RadioButtonList myRbl,string valueField,string TextField)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
myRbl.DataSource=dr;
myRbl.DataValueField=valueField;
myRbl.DataTextField=TextField;
myRbl.DataBind();
dr.Close();
}
/// <summary>
/// 所有数据来自数据库
/// </summary>
/// <param name="str">SQL</param>
/// <param name="myCbl">Cbl</param>
/// <param name="valueField">Value值</param>
/// <param name="TextField">Text值</param>
/// <param name="Select">是否被选中</param>
public void BindCheckBoxListSDb(string str,CheckBoxList myCbl,string valueField,string TextField,string Select)
{
Comm=new OleDbCommand(str,Conn);
dr=Comm.ExecuteReader();
while(dr.Read())
{
ListItem Lt=new ListItem(dr[TextField].ToString(),dr[valueField].ToString());
Lt.Selected=Convert.ToBoolean(dr[Select].ToString());
myCbl.Items.Add(Lt);
}
dr.Close();
}
#endregion
#region DataSet绑定列
/*DataSet绑定列*/
/// <summary>
/// 绑定DropDownList控件并显示数据,DropDownList控件Value,Text值将等于str_Text值
/// </summary>
/// <param name="str_Text">绑定DropDownList控件Value,Text值相对应数据库表字段名</param>
/// <param name="sql">Select-SQL语句</param>
/// <param name="myDropDownList">DropDownList控件id值</param>
public void BindDropDownListDs(string strSql,DropDownList myDropDownList,string valueField,string textField)
{
Fill(strSql);
myDropDownList.DataSource=ds.Tables[0].DefaultView;
myDropDownList.DataValueField =valueField;
myDropDownList.DataTextField=textField;
myDropDownList.DataBind();
}
public void BindDataGridDs(string strSql,DataGrid myDataGrid)
{
Fill(strSql);
myDataGrid.DataSource=ds.Tables[0].DefaultView;
myDataGrid.DataBind();
}
public void BindRepeaterDs(string strSql,Repeater myRepeater)
{
Fill(strSql);
myRepeater.DataSource=ds.Tables[0].DefaultView;
myRepeater.DataBind();
}
public void BindListBox(string strSql,ListBox myListBox)
{
Fill(strSql);
myListBox.DataSource=ds.Tables[0].DefaultView;
myListBox.DataBind();
}
#endregion
/// <summary>
/// 清除HTML文件的代码和SQL制定的代码
/// </summary>
/// <param name="Str">要修改的字符串</param>
/// <returns></returns>
public string SetNOHtmlChr(string Str)
{
Str=Str.Replace("'","’");
Str=Str.Replace("<","〈");
Str=Str.Replace(">","〉");
return Str;
}
}
}