c#连接access数据库操作类分享
本文导语: 1、配置web.config文件:配置数据库连接参数(根据实际情况设置数据库路径和密码) 代码如下: 2、操作Access数据库C#类 代码如下:using System;using System.Data;using System.Data.OleDb;using System.Web;using System.Web.UI;using System.Configuration; namespace C...
1、配置web.config文件:配置数据库连接参数(根据实际情况设置数据库路径和密码)
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
{
///
/// 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;
}
}
}