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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1249
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL: From Small Businesses to Large Enterprises MySQL: From Small Businesses to Large Enterprises Apr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

See all articles