欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#连接access数据库操作类分享

程序员文章站 2024-02-18 00:00:40
1、配置web.config文件:配置数据库连接参数(根据实际情况设置数据库路径和密码) 复制代码 代码如下:

1、配置web.config文件:配置数据库连接参数(根据实际情况设置数据库路径和密码)

复制代码 代码如下:

<configuration>
<appsettings/>
<connectionstrings>
<add name="connectionstring" connectionstring="provider=microsoft.jet.oledb.4.0;data source=d:\keleyi\app_data\db.mdb;jet oledb:database password=keleyi.com"
providername="system.data.oledb" />
</connectionstrings>

2、操作access数据库c#类

复制代码 代码如下:

using system;
using system.data;
using system.data.oledb;
using system.web;
using system.web.ui;
using system.configuration;

namespace com.lxj.database
{
/// <summary>
/// conn 的摘要说明。
/// </summary>
public class conndbforacccess
{
/// <summary>
/// 连接数据库字符串
/// </summary>
private string connectionstring;

/// <summary>
/// 存储数据库连接(保护类,只有由它派生的类才能访问)
/// </summary>
protected oledbconnection connection;

/// <summary>
/// 构造函数:数据库的默认连接
/// </summary>
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);
}

/// <summary>
/// 构造函数:带有参数的数据库连接
/// </summary>
/// <param name="newconnectionstring"></param>
public conndbforacccess(string newconnectionstring)
{
//connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" + httpcontext.current.request.physicalapplicationpath + newconnectionstring;
connectionstring = newconnectionstring;
connection = new oledbconnection(connectionstring);
}

/// <summary>
/// 获得连接字符串
/// </summary>
public string connectionstring
{
get
{
return connectionstring;
}
}


/// <summary>
/// 执行sql语句没有返回结果,如:执行删除、更新、插入等操作
/// </summary>
/// <param name="strsql"></param>
/// <returns>操作成功标志</returns>
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;
}

/// <summary>
/// 执行sql语句返回结果到datareader中
/// </summary>
/// <param name="strsql"></param>
/// <returns>datareader</returns>
private oledbdatareader returndatareader(string strsql)
{
connection.open();
oledbcommand command = new oledbcommand(strsql, connection);
oledbdatareader datareader = command.executereader();
connection.close();

return datareader;
}

/// <summary>
/// 执行sql语句返回结果到dataset中
/// </summary>
/// <param name="strsql"></param>
/// <returns>dataset</returns>
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;
}

/// <summary>
/// 执行一查询语句,同时返回查询结果数目
/// </summary>
/// <param name="strsql"></param>
/// <returns>sqlresultcount</returns>
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;
}


}
}

上一篇: asp文本文件操作

下一篇: