zoukankan      html  css  js  c++  java
  • OleDbHelper

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using System.Configuration;
    namespace OleDbHelper
    {
        public static class OleDbHelper
        {
            private static OleDbConnection connection;
            /// <summary>
            /// 获得一个唯一的CONNECTION 实例
            /// </summary>
            public static OleDbConnection Connection
            {
                get
                {
                    string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=db.mdb;";
                   
                    if (connection == null)
                    {
                        connection = new OleDbConnection(connectionstring);
                        connection.Open();
                    }
                    else if (connection.State == System.Data.ConnectionState.Closed)
                    {
                        connection.Open();
    
                    }
                    else if (connection.State == System.Data.ConnectionState.Broken)
                    {
                        connection.Close();
                        connection.Open();
                    }
                    return connection;
                }
            }
            /// <summary>
            /// 返回执行SQL 语句所影响数据的行数
            /// </summary>
            /// <param name="sql">sql 语句</param>
            /// <returns>影响行数</returns>
            public static int ExecuteCommand(string sql)
            {
                OleDbCommand com = new OleDbCommand(sql, Connection);
                int result = com.ExecuteNonQuery();
                return result;
            }
            /// <summary>
            /// 获取结果集的第一行第一列
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static int GetScalar(string sql)
            {
                OleDbCommand com = new OleDbCommand(sql, Connection);
                int result = int.Parse(com.ExecuteScalar().ToString());
                return result;
            }
            /// <summary>
            ///  执行Sql语句,获取DataTable结果集
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <returns>DataTable</returns>
            public static DataTable GetDataTable(string sql)
            {
                DataSet dataset = new DataSet();
                OleDbCommand com = new OleDbCommand(sql, Connection);
                OleDbDataAdapter da = new OleDbDataAdapter(com);
                da.Fill(dataset);
                return dataset.Tables[0];
            }
            /// <summary>
            /// 执行Sql语句,获取DataSet结果集
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <returns>DataSet</returns>
            public static DataSet GetDataSet(string sql)
            {
                DataSet dataset = new DataSet();
                OleDbCommand com = new OleDbCommand(sql, Connection);
                OleDbDataAdapter da = new OleDbDataAdapter(com);
                da.Fill(dataset);
                return dataset;
            }
            /// <summary>
            /// 获取OleDbDataReader结果集
            /// </summary>
            /// <param name="sql">SQL语句</param>
            /// <returns>OleDbDataReader</returns>
            public static OleDbDataReader GetReader(string sql)
            {
                OleDbCommand com = new OleDbCommand(sql, Connection);
                OleDbDataReader reader = com.ExecuteReader();
                return reader;
            }
            /// <summary>
            /// 执行SQL语句
            /// </summary>
            /// <param name="SQLString">SQL语句</param>
            /// <param name="cmdParms">参数</param>
            /// <returns>影响行数</returns>
            public static int ExecuteSql(string SQLString, params OleDbParameter[] cmdParms)
            {
                OleDbCommand com = new OleDbCommand(SQLString, Connection);
                PrepareCommand(com, Connection, null, SQLString, cmdParms);
                int rows = com.ExecuteNonQuery();
                com.Parameters.Clear();
                return rows;
            }
            /// <summary>
            /// 获取DataSet结果集
            /// </summary>
            /// <param name="SQLString">SQL语句</param>
            /// <param name="cmdParms">参数</param>
            /// <returns>DataSet</returns>
            public static DataSet GetDataSet(string SQLString, params OleDbParameter[] cmdParms)
            {
                OleDbCommand cmd = new OleDbCommand(SQLString, Connection);
                PrepareCommand(cmd, Connection, null, SQLString, cmdParms);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataSet ds = new DataSet();
                try
                {
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    da.Fill(ds, "ds");
                    cmd.Parameters.Clear();
                }
                catch
                {
                  
                }
                finally
                {
                 
                }
                return ds;
            }
            private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, OleDbParameter[] cmdParms)
            {
                cmd.CommandText = cmdText;
                cmd.Connection = conn;
                if (trans != null)
                    cmd.Transaction = trans;
                cmd.CommandType = CommandType.Text;//cmdType;
                if (cmdParms != null)
                {
                    foreach (OleDbParameter parm in cmdParms)
                        cmd.Parameters.Add(parm);
                }
            }
        }
    }
  • 相关阅读:
    ThinkPHP 3 的CURD管理用户信息 修改和删除
    检测一个字符串是否为一个有效的编码格式字符串
    将Buffer对象结合创建为一个新的Buffer对象
    Buffer.byteLength(字符串,编码方式)计算指定字符串的字节数
    TypeError: Buffer.allocUnsafe is not a function
    多个haproxy 之间跳转
    TypeError: Identifier 'assert' has already been declared
    14.5.7 Storing InnoDB Undo Logs in Separate Tablespaces 存储InnoDB Undo logs 到单独的表空间
    14.5.5 Creating a File-Per-Table Tablespace Outside the Data Directory
    php session 管理
  • 原文地址:https://www.cnblogs.com/roboot/p/OleDbHelper.html
Copyright © 2011-2022 走看看