OleBase基类
先声明一下,偶是菜鸟。
这个类是以前自己在NET1.1里面的写的,呵呵,好在到了2.0还可以用。只是获取那个数据连接串的方法变了一下,变成一个WebConfigurationManager.AppSettings[ConnStrKey]。好象还可以在配置文件里有专用的数据连接字串,待有空再去研究这个问题了。
现在是在赶时间,争取在年前把这个网站给改了就行。
把这个类放在空间里,以方便自己使用。因为我所有对数据访问的类,都继承了这个基类。
这个类是以前自己在NET1.1里面的写的,呵呵,好在到了2.0还可以用。只是获取那个数据连接串的方法变了一下,变成一个WebConfigurationManager.AppSettings[ConnStrKey]。好象还可以在配置文件里有专用的数据连接字串,待有空再去研究这个问题了。
现在是在赶时间,争取在年前把这个网站给改了就行。
把这个类放在空间里,以方便自己使用。因为我所有对数据访问的类,都继承了这个基类。
public class OleBase
{
/// <summary>
/// SQL查询语句字串
/// </summary>
public string QuerySQL = string.Empty;
// Web.Config配置数据库连接举例
// <appSettings>
// <add key="ConnStr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\dmcweb\mdb\db1.mdb;Persist Security Info=False;Jet OLEDB:DataBase password='******'"/>
// </appSettings>
/// <summary>
/// 在Web.config文件的配置节中配置的数据库连接字符串键名
/// </summary>
public string ConnStrKey = "ConnStr";
/// <summary>
/// 只读
/// 根据配置节中的键名,返回在web.config文件中设置的数据库连接字符串
/// </summary>
public string ConnStr
{
get
{
return System.Web.Configuration.WebConfigurationManager.AppSettings[ConnStrKey];
}
}
public OleBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 据指定的查询语句及数据库连接,返回OleDbDataReader
/// </summary>
/// <returns></returns>
public OleDbDataReader GetDataReader()
{
OleDbConnection Connection = new OleDbConnection();
Connection.ConnectionString = ConnStr;
OleDbCommand Command = new OleDbCommand();
Command.CommandText = QuerySQL;
Command.Connection = Connection;
Connection.Open();
OleDbDataReader dr = Command.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
/// <summary>
/// 据指定的查询语句及数据库连接,返回DataSet
/// </summary>
/// <returns></returns>
public DataSet GetDataSet()
{
OleDbConnection Connection = new OleDbConnection();
Connection.ConnectionString = ConnStr;
OleDbCommand Command = new OleDbCommand();
Command.CommandText = QuerySQL;
Command.Connection = Connection;
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(Command);
DataSet dt = new DataSet();
Connection.Open();
dataAdapter.Fill(dt);
Connection.Close();
return dt;
}
/// <summary>
/// 据指定的查询语句及数据库连接,执行对数据的操作
/// </summary>
public void ExecuteData()
{
OleDbConnection Connection = new OleDbConnection();
Connection.ConnectionString = ConnStr;
OleDbCommand Command = new OleDbCommand();
Command.CommandText = QuerySQL;
Command.Connection = Connection;
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
}
}
{
/// <summary>
/// SQL查询语句字串
/// </summary>
public string QuerySQL = string.Empty;
// Web.Config配置数据库连接举例
// <appSettings>
// <add key="ConnStr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\dmcweb\mdb\db1.mdb;Persist Security Info=False;Jet OLEDB:DataBase password='******'"/>
// </appSettings>
/// <summary>
/// 在Web.config文件的配置节中配置的数据库连接字符串键名
/// </summary>
public string ConnStrKey = "ConnStr";
/// <summary>
/// 只读
/// 根据配置节中的键名,返回在web.config文件中设置的数据库连接字符串
/// </summary>
public string ConnStr
{
get
{
return System.Web.Configuration.WebConfigurationManager.AppSettings[ConnStrKey];
}
}
public OleBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 据指定的查询语句及数据库连接,返回OleDbDataReader
/// </summary>
/// <returns></returns>
public OleDbDataReader GetDataReader()
{
OleDbConnection Connection = new OleDbConnection();
Connection.ConnectionString = ConnStr;
OleDbCommand Command = new OleDbCommand();
Command.CommandText = QuerySQL;
Command.Connection = Connection;
Connection.Open();
OleDbDataReader dr = Command.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
/// <summary>
/// 据指定的查询语句及数据库连接,返回DataSet
/// </summary>
/// <returns></returns>
public DataSet GetDataSet()
{
OleDbConnection Connection = new OleDbConnection();
Connection.ConnectionString = ConnStr;
OleDbCommand Command = new OleDbCommand();
Command.CommandText = QuerySQL;
Command.Connection = Connection;
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(Command);
DataSet dt = new DataSet();
Connection.Open();
dataAdapter.Fill(dt);
Connection.Close();
return dt;
}
/// <summary>
/// 据指定的查询语句及数据库连接,执行对数据的操作
/// </summary>
public void ExecuteData()
{
OleDbConnection Connection = new OleDbConnection();
Connection.ConnectionString = ConnStr;
OleDbCommand Command = new OleDbCommand();
Command.CommandText = QuerySQL;
Command.Connection = Connection;
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
}
}