ole辅助类-sqlhelper-access
namespace dal
{
public class olehelp
{
private OleDbConnection conn = null;
private OleDbCommand cmd = null;
private OleDbDataReader oddr = null;
public olehelp()
{
conn = new OleDbConnection(ConfigurationManager.AppSettings["connstring"].ToString());
}
private OleDbConnection getconn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
/// <summary>
/// 执行不带参数的增删改语句
/// </summary>
/// <param name="cmdtext">增删改语句</param>
/// <param name="ct">命令行模式</param>
/// <returns>数值</returns>
public int execnonquery(string cmdtext, CommandType ct)
{
int res;
try
{
cmd = new OleDbCommand(cmdtext, getconn());
cmd.CommandType = ct;
res = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return res;
}
/// <summary>
/// 执行带参数的增删改语句
/// </summary>
/// <param name="cmdtext"></param>
/// <param name="paras"></param>
/// <param name="ct"></param>
/// <returns></returns>
public int execnonquery(string cmdtext, OleDbParameter[] paras, CommandType ct)
{
int res;
using (cmd = new OleDbCommand(cmdtext, getconn()))
{
cmd.Parameters.AddRange(paras);
cmd.CommandType = ct;
res = cmd.ExecuteNonQuery();
}
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
return res;
}
/// <summary>
/// 执行不带参数查询语句
/// </summary>
/// <param name="cmdtext">语句</param>
/// <param name="ct">命令格式</param>
/// <returns>数据表</returns>
public DataTable execquery(string cmdtext, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new OleDbCommand(cmdtext, getconn());
cmd.CommandType = ct;
using (oddr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(oddr);
}
return dt;
}
/// <summary>
/// 执行带参数的查询语句
/// </summary>
/// <param name="cmdtext">语句</param>
/// <param name="paras">参数</param>
/// <param name="ct">命令格式</param>
/// <returns>数据表</returns>
public DataTable execquery(string cmdtext, OleDbParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new OleDbCommand(cmdtext, getconn());
cmd.Parameters.AddRange(paras);
cmd.CommandType = ct;
using (oddr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(oddr);
}
return dt;
}
}
}
要附加上这些:
using System.Data.OleDb;
using System.Data;
using System.Configuration;