C# AccessHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

public class AccessHelper:IDisposable
    {
        private OleDbConnection m_conn = new OleDbConnection();

        public static string DATABASE = AppDomain.CurrentDomain.BaseDirectory + "rate.accdb";

        public AccessHelper()
        {
            string connstr = "Provider = Microsoft.ACE.OLEDB.12.0;";
            connstr += "Data Source =\"" + DATABASE + "\"";
            m_conn.ConnectionString = connstr;
            m_conn.Open();
        }

        public void ExecuteCommand(string sql)
        {
            OleDbCommand cmd = null;
            lock (cmd=new OleDbCommand())
            {
                cmd.Connection = m_conn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
        }

        public void ExecuteCommand(string sql, OleDbParameter[] ps)
        {
            OleDbCommand cmd = null;
            lock (cmd = new OleDbCommand())
            {
                cmd.Connection = m_conn;
                cmd.CommandText = sql;
                foreach (OleDbParameter p in ps)
                {
                    cmd.Parameters.Add(p);
                }
                cmd.ExecuteNonQuery();
            }
        }

        public object QuerySome(string sql)
        {
            OleDbCommand cmd = null;
            lock (cmd = new OleDbCommand())
            {
                cmd.Connection = m_conn;
                cmd.CommandText = sql;
                using (OleDbDataReader or = cmd.ExecuteReader())
                {
                    if (or.Read())
                    {
                        return or.GetValue(0);
                    }
                }
            }
            return null;
        }

        public DataTable GetSource(string strsql)
        {
            DataTable dt = null;
            OleDbCommand cmd = null;
            OleDbDataAdapter ad = null;
            try
            {
                lock (dt = new DataTable())
                {
                    cmd = new OleDbCommand(strsql, m_conn);
                    ad = new OleDbDataAdapter((OleDbCommand)cmd);
                    dt.Clear();
                    ad.Fill(dt);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return dt;
        }

        void IDisposable.Dispose()
        {
            m_conn.Close();
            m_conn.Dispose();
            m_conn = null;
        }
    }
时间: 2024-10-07 17:40:48

C# AccessHelper的相关文章

AccessHelper

原文:AccessHelper 代码: using System; using System.Data; using System.Configuration; using System.Data.OleDb; using ahwildlife.Utils; /// <summary> /// AccessHelper 的摘要说明 /// </summary> public class AccessHelper { #region 变量 protected static OleDb

C# AccessHelper 完整示例代码

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data; public class AccessHelper:IDisposable { private OleDbConnection m_conn = new OleDbConnection(); public static string DAT

一个可用的AccessHelper类

前些日子做项目用了下Access数据库,看了很多AccessHelper,感觉这个虽然不是最好的,却是可用的. 故此分享一下,以后看到更好的还会继续分享. using System; using System.Data; using System.Configuration; using System.Data.OleDb; using System.Collections; namespace DAL { /// <summary> /// AcceHelper 的摘要说明 /// <

C#的Access数据库操作 AccessHelper类

由于学校的安全级别提高了,我们以前做的网站使用拼接sql方法查询数据会被拦截,所以说网站就必须重构,在重构之余,复习了下AccessHelper类,随便复习了下C#数据库操作.下面我们就开始吧. 先说个工具类,就是池类Pool,很简单的一个类,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.

AccessHelper类

1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Data.OleDb; 5 using System.Collections; 6 using System.Windows.Forms; 7 using System.Security.Cryptography; 8 using System.Text; 9 /// <summary> 10 /// AcceHelper 的摘要说

Accesshelper.cs

using System; using System.Data; using System.Data.OleDb; using System.Collections; using System.IO; using System.Globalization; using System.Configuration; namespace ArticleManage { public class DataAccess { 定义#region 定义 protected OleDbCommand Comm;

Access大数据高效分页语句

oracle的分页查询可以利用rowid伪列. db2的分页查询可以利用row_number() over()聚合函数. mysql有limit. access仿佛先天缺陷,仅提供了top n.那如何利用top来实现分页查询呢? 假设在access中有表t1 createtable t1( tc1 varchar(50)notnullprimarykey, tc2 varchar(30), tc3 varchar(30) ) 随机插入20条数据.如果以每页5条来显示数据,如果要显示11至15条如

C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration; namespace XXX{    /// <summary>    /// 针对SQL Server数据库操作的通用类           /// </sum

SQLHelper帮助类

通过分装的方法: public class SQLHelper { private static readonly string ConnectionString = ConfigurationManager.AppSettings["conn"].ToString(); //SqlParameter[] 方便传递数组 /// <summary> /// 主要用于封装Command对象的ExecuteNonQuery方法,用于数据的增删改 /// </summary&