欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2021-01-07 11:18:42|栏目:.NET代码|点击:

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;
}


}
}

上一篇:C# winform点击生成二维码实例代码

栏    目:.NET代码

下一篇:asp.net Silverlight中的模式窗体

本文标题:c#连接access数据库操作类分享

本文地址:http://www.codeinn.net/misctech/41666.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有