access数据访问类
广告投放★自助友情CMS落伍广告联盟晒乐广告联盟脉动广告联盟品味广告联盟
广告位可自定样式联系QQ:4285248个文字广告月20元广告联系QQ:428524广告位可自定样式
8个文字广告月20元黄金广告位每月20元广告位可自定样式联系QQ:428524广告位可自定样式
左旋肉碱、全国包邮
买二送一、无效退款

文章浏览→编程相关Access→access数据访问类

access数据访问类
access数据访问类

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
namespace Dal
{
    publicsealed class OleHelper
    {
       private OleHelper()
       { }

       public static OleDbConnection Conn
       {
           get
           {
               return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + AppDomain.CurrentDomain.BaseDirectory +ConfigurationSettings.AppSettings["DataName"]);
           }
       }

       public static DataSet ExecuteDataset(OleDbConnection connection,CommandType commandType, string commandText)
       {
           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, null, commandType,commandText);

           OleDbDataAdapter da = new OleDbDataAdapter(cmd);

           DataSet ds = new DataSet();

           da.Fill(ds);

           cmd.Parameters.Clear();
           connection.Close();
           return ds;
       }

       public static DataSet ExecuteDataset(OleDbConnection connection,CommandType commandType, string commandText, OleDbParameter[]parameters)
       {

           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, commandType, commandText,parameters);

           DataSet retval = cmd.ExecuteDataset();

           cmd.Parameters.Clear();
           connection.Close();
           return retval;

       }

       public static int ExecuteNonQuery(OleDbConnection connection,CommandType commandType, string commandText)
       {

           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, null, commandType,commandText);

           int retval = cmd.ExecuteNonQuery();

           cmd.Parameters.Clear();
           connection.Close();
           return retval;

       }

       public static int ExecuteNonQuery(OleDbConnection connection,CommandType commandType, string commandText, OleDbParameter[]parameters)
       {

           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, commandType, commandText,parameters);

           int retval = cmd.ExecuteNonQuery();

           cmd.Parameters.Clear();
           connection.Close();
           return retval;

       }


       public static object ExecuteScalar(OleDbConnection connection,CommandType commandType, string commandText)
       {

           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, null, commandType,commandText);

           object retval = cmd.ExecuteScalar();

           cmd.Parameters.Clear();
           connection.Close();
           return retval;

       }

       public static object ExecuteScalar(OleDbConnection connection,CommandType commandType, string commandText, OleDbParameter[]parameters)
       {

           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, commandType, commandText,parameters);

           object retval = cmd.ExecuteScalar();

           cmd.Parameters.Clear();
           connection.Close();
           return retval;

       }

       public static OleDbDataReader ExecuteReader(OleDbConnectionconnection, CommandType commandType, string commandText)
       {

           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, null, commandType,commandText);

           OleDbDataReader retval = cmd.ExecuteReader();

           cmd.Parameters.Clear();
           connection.Close();
           return retval;

       }

       public static OleDbDataReader ExecuteReader(OleDbConnectionconnection, CommandType commandType, stringcommandText,OleDbParameter[] parameters)
       {

           OleDbCommand cmd = new OleDbCommand();

           PrepareCommand(cmd, connection, commandType, commandText,parameters);

           OleDbDataReader retval = cmd.ExecuteReader();

           cmd.Parameters.Clear();
           connection.Close();
           return retval;

       }

       private static void PrepareCommand(OleDbCommand command,OleDbConnection connection, OleDbTransaction transaction,CommandType commandType, string commandText)
         {
           if (connection.State != ConnectionState.Open)
           {
               connection.Open();
           }

           command.Connection = connection;

           command.CommandText = commandText;

           if (transaction != null)
         {
               command.Transaction = transaction;
           }

           command.CommandType = commandType;

           return;
       }

       private static void PrepareCommand(OleDbCommand command,OleDbConnection connection, CommandType commandType, stringcommandText,OleDbParameter[] parameters)
       {
           if (connection.State != ConnectionState.Open)
           {
               connection.Open();
           }

           command.Connection = connection;

           command.CommandText = commandText;

           command.Parameters = parameters;

           command.CommandType = commandType;

           return;
       }
    }
}

所属分类:编程相关Access    作者:新浪博客    时间:2010-11-20 0:00:00

文章导航