c#.net连接access操作类 1、配置web.config文件:配置数据库连接参数 configuration appsettings/ connectionstrings add name=connectionstring connectionstring=provider=microsoft.jet.oledb.4.0;data source=f:\teachersystem\app_data\db.mdb;jet o
c#.net连接access操作类
1、配置web.config文件:配置数据库连接参数
providername=system.data.oledb />
2
程序设计开始:
1、按要求建立access数据库及数据表
2、编写数据库访问、操作的公用类,此类可以复用到以后开发的任何系统中
(1)、新建一个c# 类库项目, 命名为“com.lxj”,设置项目属性:程序集名称、默认命名空间均为“com.lxj”
(2)、在此项目目录下创建目录database,新建c# 类文件conndbforaccess.cs 在database目录下。
添加引用:system.web.dll
(3)、编写conndbforaccess.cs 的代码
using system;
using system.data;
using system.data.oledb;
using system.web;
using system.web.ui;
using system.configuration;
namespace com.lxj.database
{
///
/// conn 的摘要说明。
///
public class conndbforacccess
{
///
/// 连接数据库字符串
///
private string connectionstring;
///
/// 存储数据库连接(保护类,只有由它派生的类才能访问)
///
protected oledbconnection connection;
///
/// 构造函数:数据库的默认连接
///
public conndbforacccess()
{
string connstr;
connstr = configurationmanager.connectionstrings[connectionstring].connectionstring.tostring();
// connstr = system.configuration.configurationsettings.appsettings[connectionstring].tostring(); //从web.config配置中读取
connectionstring = connstr;
//connectionstring = provider=microsoft.jet.oledb.4.0;data source= + httpcontext.current.request.physicalapplicationpath + connstr;
// connectionstring = system.configuration.configurationsettings.appsettings[connectionstring].tostring();
//
connection = new oledbconnection(connectionstring);
}
///
/// 构造函数:带有参数的数据库连接
///
///
public conndbforacccess(string newconnectionstring)
{
//connectionstring = provider=microsoft.jet.oledb.4.0;data source= + httpcontext.current.request.physicalapplicationpath + newconnectionstring;
connectionstring = newconnectionstring;
connection = new oledbconnection(connectionstring);
}
///
/// 获得连接字符串
///
public string connectionstring
{
get
{
return connectionstring;
}
}
///
/// 执行sql语句没有返回结果,如:执行删除、更新、插入等操作
///
///
/// 操作成功标志
public bool exesql(string strsql)
{
bool resultstate = false;
connection.open();
oledbtransaction mytrans = connection.begintransaction();
oledbcommand command = new oledbcommand(strsql, connection, mytrans);
try
{
command.executenonquery();
mytrans.commit();
resultstate = true;
}
catch
{
mytrans.rollback();
resultstate = false;
}
finally
{
connection.close();
}
return resultstate;
}
///
/// 执行sql语句返回结果到datareader中
///
///
/// datareader
private oledbdatareader returndatareader(string strsql)
{
connection.open();
oledbcommand command = new oledbcommand(strsql, connection);
oledbdatareader datareader = command.executereader();
connection.close();
return datareader;
}
///
/// 执行sql语句返回结果到dataset中
///
///
/// dataset
public dataset returndataset(string strsql)
{
connection.open();
dataset dataset = new dataset();
oledbdataadapter oledbda = new oledbdataadapter(strsql, connection);
oledbda.fill(dataset, objdataset);
connection.close();
return dataset;
}
///
/// 执行一查询语句,同时返回查询结果数目
///
///
/// sqlresultcount
public int returnsqlresultcount(string strsql)
{
int sqlresultcount = 0;
try
{
connection.open();
oledbcommand command = new oledbcommand(strsql, connection);
oledbdatareader datareader = command.executereader();
while (datareader.read())
{
sqlresultcount++;
}
datareader.close();
}
catch
{
sqlresultcount = 0;
}
finally
{
connection.close();
}
return sqlresultcount;
}
}//
}//
好了,数据库访问、数据库操作的公用类完成了,详细代码意义大家自己看吧,这些属于c# 语法知识了,现在可以编译生成项目了,编译完后把项目的dll文件com.lxj.dll拷贝到example项目下的bin目录中,然后在项目 example中引入com.lxj.dll即可。稍后介绍如何使用它,休息一下,喝口水,呵呵.....