[C#]ACCESS操作类
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMS.Common
{
public static class AccessDBhelper
{
private static string DBPath = CMSConfig.DBPath;
private static OleDbConnection Con;
private static OleDbCommand Com;
/// <summary>
/// 初始化连接实例
/// </summary>
private static void AccessC()
{
Con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + DBPath);
Com = new OleDbCommand();
}
/// <summary>
/// 执行SQL语句并返回所影响的行数
/// </summary>
/// <param name="sqlCommand"></param>
/// <returns></returns>
public static int cExecuteNonQuery(string sqlCommand)
{
try
{
AccessC();
Com.CommandText = sqlCommand;
Com.Connection = Con;
Con.Open();
int i = 0;
i = Com.ExecuteNonQuery();
CloseCon();
return i;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
/// <summary>
/// 执行SQL语句并返回所影响的行数
/// </summary>
/// <param name="sqlCommand"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static int cExecuteNonQuery(string sqlCommand,OleDbParameter[] paras)
{
try
{
AccessC();
Com.CommandText = sqlCommand;
FillParameter(paras);
Com.Connection = Con;
Con.Open();
int i = 0;
i = Com.ExecuteNonQuery();
CloseCon();
return i;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
private static void FillParameter(OleDbParameter[] paras)
{
foreach (OleDbParameter item in paras)
{
Com.Parameters.Add(item);
}
}
/// <summary>
/// 统计特定条件的数量
/// </summary>
/// <param name="sqlCommand"></param>
/// <returns></returns>
public static int cExecteCount(string sqlCommand)
{
try
{
AccessC();
Com.CommandText = sqlCommand;
Com.Connection = Con;
Con.Open();
int i = 0;
i = (int)Com.ExecuteScalar();
CloseCon();
return i;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
/// <summary>
/// 统计特定条件的数量
/// </summary>
/// <param name="sqlCommand"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static int cExecteCount(string sqlCommand,OleDbParameter[] paras)
{
try
{
AccessC();
Com.CommandText = sqlCommand;
FillParameter(paras);
Com.Connection = Con;
Con.Open();
int i = 0;
i = (int)Com.ExecuteScalar();
CloseCon();
return i;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
/// <summary>
/// 执行SQL语句,并返回查询结果集的第一行的第一列
/// </summary>
/// <param name="sqlCommand"></param>
/// <returns></returns>
public static object cExecuteScalar(string sqlCommand)
{
try
{
AccessC();
Com.CommandText = sqlCommand;
Com.Connection = Con;
Con.Open();
object o = null;
o = (object)Com.ExecuteScalar();
CloseCon();
return o;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
/// <summary>
/// 执行SQL语句,并返回查询结果集的第一行的第一列
/// </summary>
/// <param name="sqlCommand"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object cExecuteScalar(string sqlCommand,OleDbParameter[] paras)
{
try
{
AccessC();
Com.CommandText = sqlCommand;
FillParameter(paras);
Com.Connection = Con;
Con.Open();
object o = null;
o = (object)Com.ExecuteScalar();
CloseCon();
return o;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
/// <summary>
/// 执行SQL语句并返回DataTable
/// </summary>
/// <param name="sqlCommand"></param>
/// <returns></returns>
public static DataTable cExecuteGetTable(string sqlCommand)
{
try
{
AccessC();
DataTable dt = new DataTable();
Com.CommandText = sqlCommand;
Com.Connection = Con;
OleDbDataAdapter dad = new OleDbDataAdapter(Com);
dad.Fill(dt);
CloseCon();
return dt;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
/// <summary>
/// 执行SQL语句并返回DataTable
/// </summary>
/// <param name="sqlCommand"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static DataTable cExecuteGetTable(string sqlCommand,OleDbParameter[] paras)
{
try
{
AccessC();
DataTable dt = new DataTable();
Com.CommandText = sqlCommand;
FillParameter(paras);
Com.Connection = Con;
OleDbDataAdapter dad = new OleDbDataAdapter(Com);
dad.Fill(dt);
CloseCon();
return dt;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
}
/// <summary>
/// 关闭连接
/// </summary>
private static void CloseCon()
{
Com.Dispose();
Con.Close();
}
}
}