/// <summary>
/// 连接Access数据库帮助
/// <appSettings>
/// <add key="DbPath" value="C:\AddressList.mdb"/>
/// </appSettings>
/// </summary>
public class AccessHelper
{
private static string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
ConfigurationManager.AppSettings["DbPath"];
/// <summary>
/// 设置数据库连接字符串
/// </summary>
public static string ConnectionString
{
get { return connString; }
set { connString = value; }
}
#region 非连接式查询操作的方法,用于获取多条查询记录
/// <summary>
///
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static DataTable ExecuteDataTable(string commandText, OleDbParameter[] parameters)
{
DataTable data = new DataTable();//实例化DataTable,用于装载查询结果集
using (OleDbConnection connection = new OleDbConnection(connString))
{
using (OleDbCommand command = new OleDbCommand(commandText, connection))
{
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (OleDbParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
//通过包含查询SQL的OleDbCommand实例化来实例化OleDbDataAdapter
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(data);
}
}
return data;
}
public static DataTable ExecuteDataTable(string commandText)
{
return ExecuteDataTable(commandText, null);
}
#endregion
#region 连接式查询
/// <summary>
///
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string commandText, OleDbParameter[] parameters)
{
OleDbConnection connection=new OleDbConnection(connString);
OleDbCommand command=new OleDbCommand(commandText,connection);
//如果同时传入了参数,则添加这些参数
if (parameters!=null)
{
foreach (OleDbParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();
//CommandBehavior.CloseConnection参数指示关闭Reader对象时关闭与其关联的Connection对象
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
/// <summary>
///
/// </summary>
/// <param name="commandText"></param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string commandText)
{
return ExecuteReader(commandText, null);
}
#endregion
#region 从数据库中检索单个值
/// <summary>
///
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static object ExecuteScalar(string commandText, OleDbParameter[] parameters)
{
object result = null;
using (OleDbConnection connection=new OleDbConnection(connString))
{
using (OleDbCommand command=new OleDbCommand(commandText,connection))
{
//如果同时传入了参数,则添加这些参数
if (parameters!=null)
{
foreach (OleDbParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
result = command.ExecuteScalar();
}
}
//返回查询结果的第一行第一列,忽略其他行和列
return result;
}
/// <summary>
///
/// </summary>
/// <param name="commandText"></param>
/// <returns></returns>
public static object ExecuteScalar(string commandText)
{
return ExecuteScalar(commandText, null);
}
#endregion
#region 对数据库执行增、删、改操作
/// <summary>
/// 对数据库执行增、删、改操作
/// </summary>
/// <param name="commandText"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string commandText, OleDbParameter[] parameters)
{
int count = 0;
using (OleDbConnection connection=new OleDbConnection(connString))
{
using (OleDbCommand command=new OleDbCommand(commandText,connection))
{
//如果同时传入了参数,则添加这些参数
if (parameters!=null);
{
foreach (OleDbParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
count = command.ExecuteNonQuery();
}
}
//返回执行增、删、改操作之后,数据库中受影响的行数
return count;
}
/// <summary>
/// 返回执行增、删、改操作之后,数据库中受影响的行数
/// </summary>
/// <param name="commandText"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string commandText)
{
return ExecuteNonQuery(commandText, null);
}
#endregion
}