Home Database Mysql Tutorial 也谈OleDbHelper,由SqlHelper类转OleDbHelper类,access操作

也谈OleDbHelper,由SqlHelper类转OleDbHelper类,access操作

Jun 07, 2016 pm 03:05 PM
ac

今意外看见一贴子谈OleDbHelper,突然想起自已一直用的由SqlHelper类转成的OleDbHelper,在此分享,自己用了二年了,还没发现异常, 代码如下: using System; using System.Data; using System.Data.OleDb; using System.Configuration; using System.Colle

    今意外看见一贴子谈OleDbHelper,突然想起自已一直用的由SqlHelper类转成的OleDbHelper,在此分享,自己用了二年了,还没发现异常, 代码如下:

  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Data.Sql;
  7. using System.Text;
  8. namespace Lihui.Common
  9. {
  10.     /// 
  11.     /// Summary description for OleDbHelper
  12.     /// 
  13.     public class OleDbHelper
  14.     {
  15.         //Database connection strings
  16.         public static readonly string CONN_STRING = ConfigurationManager.AppSettings["OleDbConnectionString"];
  17.         public static readonly string CONN_STRING1 = ConfigurationManager.AppSettings["OleDbConnectionString1"];
  18.         // Hashtable to store cached parameters
  19.         private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
  20.         #region =ExecuteNonQuery=
  21.         public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText)
  22.         {
  23.             return ExecuteNonQuery(connString, cmdType, cmdText, null);
  24.         }
  25.         public static int ExecuteNonQuery(OleDbConnection conn, CommandType cmdType, string cmdText)
  26.         {
  27.             return ExecuteNonQuery(conn, cmdType, cmdText, null);
  28.         }
  29.         public static int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText)
  30.         {
  31.             return ExecuteNonQuery(trans, cmdType, cmdText, null);
  32.         }
  33.         public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  34.         {
  35.             OleDbCommand cmd = new OleDbCommand();
  36.             using (OleDbConnection conn = new OleDbConnection(connString))
  37.             {
  38.                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  39.                 int val = cmd.ExecuteNonQuery();
  40.                 //清除cmd的参数
  41.                 cmd.Parameters.Clear();
  42.                 if (conn.State == ConnectionState.Open)
  43.                 {
  44.                     conn.Close();
  45.                 }
  46.                 return val;
  47.             }
  48.         }
  49.         public static int ExecuteNonQuery(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  50.         {
  51.             OleDbCommand cmd = new OleDbCommand();
  52.             PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  53.             int val = cmd.ExecuteNonQuery();
  54.             cmd.Parameters.Clear();
  55.             if (conn.State == ConnectionState.Open)
  56.             {
  57.                 conn.Close();
  58.             }
  59.             return val;
  60.         }
  61.         public static int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  62.         {
  63.             OleDbCommand cmd = new OleDbCommand();
  64.             PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
  65.             int val = cmd.ExecuteNonQuery();
  66.             cmd.Parameters.Clear();
  67.             if (cmd.Connection.State == ConnectionState.Open)
  68.             {
  69.                 cmd.Connection.Close();
  70.             }
  71.             return val;
  72.         }
  73.         #endregion
  74.         #region =ExecuteReader=
  75.         public static OleDbDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
  76.         {
  77.             //pass through the call providing null for the set of OleDbParameters
  78.             return ExecuteReader(connectionString, commandType, commandText, (OleDbParameter[])null);
  79.         }
  80.         public static OleDbDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  81.         {
  82.             OleDbCommand cmd = new OleDbCommand();
  83.             OleDbConnection conn = new OleDbConnection(connString);
  84.             try
  85.             {
  86.                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  87.                 OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  88.                 cmd.Parameters.Clear();
  89.                 if (conn.State == ConnectionState.Open)
  90.                 {
  91.                     conn.Close();
  92.                 }
  93.                 return rdr;
  94.             }
  95.             catch
  96.             {
  97.                 conn.Close();
  98.                 throw;
  99.             }
  100.         }
  101.         #endregion
  102.         #region =ExecuteDataset=
  103.         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
  104.         {
  105.             return ExecuteDataset(connectionString, commandType, commandText, (OleDbParameter[])null);
  106.         }
  107.         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
  108.         {
  109.             using (OleDbConnection cn = new OleDbConnection(connectionString))
  110.             {
  111.                 cn.Open();
  112.                 //调用重载方法
  113.                 return ExecuteDataset(cn, commandType, commandText, commandParameters);
  114.             }
  115.         }
  116.         public static DataSet ExecuteDataset(OleDbConnection connection, CommandType commandType, string commandText)
  117.         {
  118.             return ExecuteDataset(connection, commandType, commandText, (OleDbParameter[])null);
  119.         }
  120.         public static DataSet ExecuteDataset(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
  121.         {
  122.             //创建一个OleDbCommand对象,并对其进行初始化
  123.             OleDbCommand cmd = new OleDbCommand();
  124.             PrepareCommand(cmd, connection, (OleDbTransaction)null, commandType, commandText, commandParameters);
  125.             //创建OleDbDataAdapter对象以及DataSet
  126.             OleDbDataAdapter da = new OleDbDataAdapter(cmd);
  127.             DataSet ds = new DataSet();
  128.             //填充ds
  129.             da.Fill(ds);
  130.             // 清除cmd的参数集合   
  131.             cmd.Parameters.Clear();
  132.             if (cmd.Connection.State == ConnectionState.Open)
  133.             {
  134.                 cmd.Connection.Close();
  135.             }
  136.             //返回ds
  137.             return ds;
  138.         }
  139.         #endregion
  140.         #region =ExecuteDataTable=
  141.         public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText)
  142.         {
  143.             return ExecuteDataTable(connectionString, commandType, commandText, (OleDbParameter[])null);
  144.         }
  145.         public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
  146.         {
  147.             using (OleDbConnection cn = new OleDbConnection(connectionString))
  148.             {
  149.                 cn.Open();
  150.                 //调用重载方法
  151.                 return ExecuteDataTable(cn, commandType, commandText, commandParameters);
  152.             }
  153.         }
  154.         public static DataTable ExecuteDataTable(OleDbConnection connection, CommandType commandType, string commandText)
  155.         {
  156.             return ExecuteDataTable(connection, commandType, commandText, (OleDbParameter[])null);
  157.         }
  158.         public static DataTable ExecuteDataTable(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
  159.         {
  160.             //创建一个OleDbCommand对象,并对其进行初始化
  161.             OleDbCommand cmd = new OleDbCommand();
  162.             PrepareCommand(cmd, connection, (OleDbTransaction)null, commandType, commandText, commandParameters);
  163.             //创建OleDbDataAdapter对象以及DataSet
  164.             OleDbDataAdapter da = new OleDbDataAdapter(cmd);
  165.             DataSet ds = new DataSet();
  166.             //填充ds
  167.             da.Fill(ds);
  168.             // 清除cmd的参数集合   
  169.             cmd.Parameters.Clear();
  170.             if (cmd.Connection.State == ConnectionState.Open)
  171.             {
  172.                 cmd.Connection.Close();
  173.             }
  174.             //返回ds
  175.             return ds.Tables[0];
  176.         }
  177.         #endregion
  178.         #region =ExecuteScalar=
  179.         public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText)
  180.         {
  181.             return ExecuteScalar(connString, cmdType, cmdText, null);
  182.         }
  183.         public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  184.         {
  185.             OleDbCommand cmd = new OleDbCommand();
  186.             using (OleDbConnection conn = new OleDbConnection(connString))
  187.             {
  188.                 PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  189.                 object val = cmd.ExecuteScalar();
  190.                 cmd.Parameters.Clear();
  191.                 if (conn.State == ConnectionState.Open)
  192.                 {
  193.                     conn.Close();
  194.                 }
  195.                 return val;
  196.             }
  197.         }
  198.         public static object ExecuteScalar(OleDbConnection conn, CommandType cmdType, string cmdText)
  199.         {
  200.             return ExecuteScalar(conn, cmdType, cmdText, null);
  201.         }
  202.         public static object ExecuteScalar(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
  203.         {
  204.             OleDbCommand cmd = new OleDbCommand();
  205.             PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
  206.             object val = cmd.ExecuteScalar();
  207.             cmd.Parameters.Clear();
  208.             if (conn.State == ConnectionState.Open)
  209.             {
  210.                 conn.Close();
  211.             }
  212.             return val;
  213.         }
  214.         #endregion
  215.         public static void CacheParameters(string cacheKey, params OleDbParameter[] cmdParms)
  216.         {
  217.             parmCache[cacheKey] = cmdParms;
  218.         }
  219.         public static OleDbParameter[] GetCachedParameters(string cacheKey)
  220.         {
  221.             OleDbParameter[] cachedParms = (OleDbParameter[])parmCache[cacheKey];
  222.             if (cachedParms == null)
  223.                 return null;
  224.             OleDbParameter[] clonedParms = new OleDbParameter[cachedParms.Length];
  225.             for (int i = 0, j = cachedParms.Length; i 
  226.                 clonedParms[i] = (OleDbParameter)((ICloneable)cachedParms[i]).Clone();
  227.             return clonedParms;
  228.         }
  229.         public static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
  230.         {
  231.             //判断连接的状态。如果是关闭状态,则打开
  232.             if (conn.State != ConnectionState.Open)
  233.                 conn.Open();
  234.             //cmd属性赋值
  235.             cmd.Connection = conn;
  236.             cmd.CommandText = cmdText;
  237.             //是否需要用到事务处理
  238.             if (trans != null)
  239.                 cmd.Transaction = trans;
  240.             cmd.CommandType = cmdType;
  241.             //添加cmd需要的存储过程参数
  242.             if (cmdParms != null)
  243.             {
  244.                 foreach (OleDbParameter parm in cmdParms)
  245.                     cmd.Parameters.Add(parm);
  246.             }
  247.         }
  248.     }
  249. }
    使用方法跟SqlHelper类相似。  
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles