时间:2021-07-01 10:21:17 帮助过:4人阅读
原文发布时间为:2009-10-28 —— 来源于本人的百度文章 [由搬家工具导入]
using System;
using System.Data;
using System.Data.OleDb;
public class AccessHelper
{
private string configName = string.Empty;
public AccessHelper()
{
this.configName = "conn";//默认值
}
/// <summary>
/// 实例化。http://hi.baidu.com/handboy
/// </summary>
/// <param name="ConfigName">配置文件Add的name属性值。</param>
public AccessHelper(string ConfigName)
{
this.configName = ConfigName;
}
/// <summary>
/// 数据库连接。
/// </summary>
private string ConnectionValue
{
get
{
return System.Configuration.ConfigurationManager.ConnectionStrings[configName].ConnectionString;
}
}
/// <summary>
/// 用于执行一段SQL语句。
/// default Command type is text
/// </summary>
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
/// <param name="cmdParms">参数列表 (Paramters)</param>
/// <returns>返回影响行数 (effect line number)</returns>
public int ExecuteNonQuery(string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(ConnectionValue))
{
PrepareCommand(cmd, conn, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 用于执行一段SQL语句。
/// To excute a SQL statement, which reuturns a integer stand for effect line number.
/// </summary>
/// <param name="cmdType">command的字符串 (SQL Statement)</param>
/// <returns>返回影响行数 (effect line number)</returns>
public int ExecuteNonQuery(string cmdText)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(ConnectionValue))
{
PrepareCommand(cmd, conn,cmdText, null);
int val = cmd.ExecuteNonQuery();
return val;
}
}
/// <summary>
/// 用于执行一个Select语句返回一个datareader
/// To excute a SQL statement, and reuturns a dataReader.
/// default command type is text
/// </summary>
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
/// <param name="cmdParms">参数列表 (Paramters)</param>
/// <returns>datareader</returns>
public OleDbDataReader ExecuteReader(string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
OleDbConnection conn = new OleDbConnection(ConnectionValue);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn,cmdText, cmdParms);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 用于执行一个Select语句返回一个datareader
/// To excute a SQL statement, and reuturns a dataReader.
/// </summary>
/// <param name="cmdType">command的字符串 (SQL Statement)</param>
/// <returns>dataReader</returns>
public OleDbDataReader ExecuteReader(string cmdText)
{
OleDbCommand cmd = new OleDbCommand();
OleDbConnection conn = new OleDbConnection(ConnectionValue);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand(cmd, conn, cmdText, null);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 用于读取一个值,查询所返回的是结果集中第一行的第一列
/// To excute a SQL statement, and returns the first column of the first line
/// Default command type is text
/// </summary>
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
/// <param name="cmdParms">参数列表 (Paramters)</param>
/// <returns>the first column of the first line</returns>
public string ExecuteScalar(string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(ConnectionValue))
{
PrepareCommand(cmd, conn,cmdText, cmdParms);
string val = Convert.ToString(cmd.ExecuteScalar());
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 用于读取一个值,查询所返回的是结果集中第一行的第一列
/// To excute a SQL statement, and returns the first column of the first line
/// </summary>
/// <param name="cmdType">command的字符串 (SQL Statement)</param>
/// <param name="cmdText">command的类型,具体见:CommandType (Command type)</param>
/// <param name="cmdParms">参数列表 (Paramters)</param>
/// <returns>the first column of the first line</returns>
public string ExecuteScalar(string cmdText)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(ConnectionValue))
{
PrepareCommand(cmd, conn, cmdText, null);
string val = Convert.ToString(cmd.ExecuteScalar());
return val;
}
}
/// <summary>
/// 在执行SQL语句之前的准备工作
/// </summary>
/// <param name="cmd">command</param>
/// <param name="conn">connection</param>
/// <param name="cmdType">command类型</param>
/// <param name="cmdText">command字符串</param>
/// <param name="cmdParms">参数列表</param>
private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, string cmdText, OleDbParameter[] cmdParms)
{
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
}
}
Access数据库访问类 帮助类
标签:sql prepare throws params each close connect man turn