zoukankan      html  css  js  c++  java
  • c# access oledb helper class

    连接Access数据库

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Data.OleDb;
    
    
    namespace TestAccessDB
    {
       
       
        public class DBHelper
        {
            protected static OleDbConnection conn = new OleDbConnection();
            protected static OleDbCommand comm = new OleDbCommand();
    
            public DBHelper()
            {
               
            }
            static string dbFile = AppDomain.CurrentDomain.BaseDirectory + "\goods.mdb";
    
            private static void openConnection()
            {
                if (conn.State == ConnectionState.Closed)
                {
                    // conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + dbFile + ";Jet OLEDB:Database PassWord=sa";
                    // conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ dbFile +";Persist Security Info=False;Jet OLEDB:Database Password=sa;";
                    conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile + ";Persist Security Info=False;";
                    comm.Connection = conn;
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception e)
                    { throw new Exception(e.Message); }
    
                }
    
            }
            private static void closeConnection()
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                    comm.Dispose();
                }
            }
            /// <summary>
            /// 执行sql语句
            /// </summary>
            /// <param name="sqlstr"></param>
            public static int ExecuteNonQuery(string sqlstr)
            {
                int i = 0;
                try
                {
                    openConnection();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                   i= comm.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
    
                    closeConnection();
    
                }
                return i;
            }
    
    
            /// <summary>
            /// 执行sql语句
            /// </summary>
            /// <param name="sqlstr"></param>
            public static object  executeScalarSql(string sqlstr)
            {
                object o;
                try
                {
                    openConnection();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                     o= comm.ExecuteScalar();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
    
                    closeConnection();
    
                }
    
                return o;
            }
    
    
    
            public static int batchInsert(int count)
            {
    
                int cnt = 0;
    
                try
                {
    
                    for (int i = 1; i <= count; i++)
                    {
                        string sqlstr = "insert into article(sourceID,[user],updateDate) values ('454545434','qwertteeeew_gfhfghgfh',#" + DateTime.Now + "#)";
    
                        openConnection();
                        comm.CommandType = CommandType.Text;
                        comm.CommandText = sqlstr;
                        cnt += comm.ExecuteNonQuery();
    
                    }
    
    
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
    
                    closeConnection();
    
                }
    
                return cnt;
    
            }
    
    
    
            /// <summary>
            /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns></returns>
            public static OleDbDataReader dataReader(string sqlstr)
            {
                OleDbDataReader dr = null;
                try
                {
                    openConnection();
                    comm.CommandText = sqlstr;
                    comm.CommandType = CommandType.Text;
    
                    dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch
                {
                    try
                    {
                        dr.Close();
                        closeConnection();
                    }
                    catch { }
                }
                return dr;
            }
            /// <summary>
            /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <param name="dr"></param>
            public static void dataReader(string sqlstr, ref OleDbDataReader dr)
            {
                try
                {
                    openConnection();
                    comm.CommandText = sqlstr;
                    comm.CommandType = CommandType.Text;
                    dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch
                {
                    try
                    {
                        if (dr != null && !dr.IsClosed)
                            dr.Close();
                    }
                    catch
                    {
                    }
                    finally
                    {
                        closeConnection();
                    }
                }
            }
            /// <summary>
            /// 返回指定sql语句的dataset
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns></returns>
            public static DataSet dataSet(string sqlstr)
            {
                DataSet ds = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter();
                try
                {
                    openConnection();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                    da.SelectCommand = comm;
                    da.Fill(ds);
    
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    closeConnection();
                }
                return ds;
            }
            /// <summary>
            /// 返回指定sql语句的dataset
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <param name="ds"></param>
            public static void dataSet(string sqlstr, ref DataSet ds)
            {
                OleDbDataAdapter da = new OleDbDataAdapter();
                try
                {
                    openConnection();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                    da.SelectCommand = comm;
                    da.Fill(ds);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    closeConnection();
                }
            }
            /// <summary>
            /// 返回指定sql语句的datatable
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns></returns>
            public static DataTable dataTable(string sqlstr)
            {
                DataTable dt = new DataTable();
                OleDbDataAdapter da = new OleDbDataAdapter();
                try
                {
                    openConnection();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                    da.SelectCommand = comm;
                    da.Fill(dt);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    closeConnection();
                }
                return dt;
            }
            /// <summary>
            /// 返回指定sql语句的datatable
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <param name="dt"></param>
            public static void dataTable(string sqlstr, ref DataTable dt)
            {
                OleDbDataAdapter da = new OleDbDataAdapter();
                try
                {
                    openConnection();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                    da.SelectCommand = comm;
                    da.Fill(dt);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    closeConnection();
                }
            }
            /// <summary>
            /// 返回指定sql语句的dataview
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns></returns>
            public static DataView dataView(string sqlstr)
            {
                OleDbDataAdapter da = new OleDbDataAdapter();
                DataView dv = new DataView();
                DataSet ds = new DataSet();
                try
                {
                    openConnection();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                    da.SelectCommand = comm;
                    da.Fill(ds);
                    dv = ds.Tables[0].DefaultView;
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    closeConnection();
                }
                return dv;
            }
        }
    }
    

      

  • 相关阅读:
    Linux学习总结(十一)—— Linux常用命令:版本信息查看(RedHat、CentOS、Debian、Ubuntu、Fedora、Oracle)...
    Linux学习总结(十一)—— Linux常用命令:版本信息查看(RedHat、CentOS、Debian、Ubuntu、Fedora、Oracle)...
    Linux 操作系统原理 — 进程与线程管理
    sed 变量替换
    触类旁通:那些关于 TBL$OR$IDX$PART$NUM 的诡异案例和知识
    python 追加库路径
    python 模块
    生成器
    深入解析:DB2 V10.5新特性列式存储表的优点与缺点
    赚钱项目轻度揭秘:旧手机回收背后的暴利内情
  • 原文地址:https://www.cnblogs.com/wgscd/p/11233137.html
Copyright © 2011-2022 走看看