日期:2010-03-30  浏览次数:20378 次

Access数据库操作类AccessHelper

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Collections;
/// <summary>
/// AcceHelper 的摘要说明
/// </summary>
public static class AccessHelper
{
    //数据库连接字符串
    public static readonly string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    // 用于缓存参数的HASH表
    private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
    /// <summary>
    ///  给定连接的数据库用假设参数执行一个sql命令(不返回数据集)
    /// </summary>
    /// <param name="connectionString">一个有效的连接字符串</param>
    /// <param name="commandText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>执行命令所影响的行数</returns>
    public static int ExecuteNonQuery(string connectionString, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            PrepareCommand(cmd, conn, null, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }
    }
    /// <summary>
    /// 用现有的数据库连接执行一个sql命令(不返回数据集)
    /// </summary>
    /// <remarks>
    ///举例: 
    ///  int result = ExecuteNonQuery(connString, "PublishOrders", new OleDbParameter("@prodid", 24));
    /// </remarks>
    /// <param name="conn">一个现有的数据库连接</param>
    /// <param name="commandText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>执行命令所影响的行数</returns>
    public static int ExecuteNonQuery(OleDbConnection connection, string cmdText, params OleDbParameter[] commandParameters)
    {
        OleDbCommand cmd = new OleDbCommand();
        PrepareCommand(cmd, connection, null, cmdText, commandParameters);
        int val = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        return val;
    }
    /// <summary>
    ///使用现有的SQL事务执行一个sql命令(不返回数据集)
    /// </summary>
    /// <remarks>
    ///举例: 
    ///  int result = ExecuteNonQuery(trans, "PublishOrders", new OleDbParameter("@prodid", 24));
    /// </remarks>
    /// <param name="trans">一个现有的事务</param>
    /// <param name="commandText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>执行命令所影响的行数</returns>