有时候做数据的中转,sqlserver和oracle这些大型数据库有点杀鸡用牛刀,而且会增加维护成本,这时可以使用access数据库,尤其是处理winform的时候。下面简单说一下 access的数据访问类的使用方法,该类为静态方法,如果是多线程程序,可能会造成数据库连接之
        					     有时候做数据的中转,sqlserver和oracle这些大型数据库有点“杀鸡用牛刀”,而且会增加维护成本,这时可以使用“access”数据库,尤其是处理“winform”的时候。下面简单说一下
access的数据访问类的使用方法,该类为静态方法,如果是多线程程序,可能会造成“数据库”连接之间的竞争。比如一个线程打开了数据库连接,还没有处理完,另一个线程就要关闭,这时就不能使用这个类了。
1、类如下。
using system;
using system.data;
using system.configuration;
using system.web;
using system.data.oledb;
namespace model
{
    /// 
    /// dataaccess 的摘要说明 c#操作access实例解析 
    /// 
    public class dataaccess
    {
        protected static oledbconnection conn = new oledbconnection();
        protected static oledbcommand comm = new oledbcommand();
        public dataaccess()
        {
            //init c#操作access实例解析 
        }
        private static void openconnection()
        {
            if (conn.state == connectionstate.closed)
            {
                conn.connectionstring = @provider=microsoft.jet.oledb.4.0;data source= + configurationmanager.appsettings[myconn];
                //web.config文件里设定。  
                comm.connection = conn;
                try
                {
                    conn.open();
                }
                catch (exception e)
                { throw new exception(e.message); }
            }
        }//打开数据库 c#操作access实例解析
        private static void closeconnection()
        {
            if (conn.state == connectionstate.open)
            {
                conn.close();
                conn.dispose();
                comm.dispose();
            }
        }//关闭数据库 c#操作access实例解析 
        public static void excutesql(string sqlstr)
        {
            try
            {
                openconnection();
                comm.commandtype = commandtype.text;
                comm.commandtext = sqlstr;
                comm.executenonquery();
            }
            catch (exception e)
            {
                throw new exception(e.message);
            }
            finally
            { closeconnection(); }
        }//执行sql语句 c#操作access实例解析 
        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;
        }
        //返回指定sql语句的oledbdatareader对象,使用时请注意关闭这个对象。  
        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();
                }  //c#操作access实例解析
                catch
                {
                }
                finally
                {
                    closeconnection();
                }
            }
        }
        //返回指定sql语句的oledbdatareader对象,使用时请注意关闭  
        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;
        }//返回指定sql语句的dataset c#操作access实例解析 
        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();
            }
        }//返回指定sql语句的dataset c#操作access实例解析
        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;
        }//返回指定sql语句的datatable  
        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();
            }
        }//返回指定sql语句的datatable c#操作access实例解析 
        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;
        }
        //返回指定sql语句的dataview c#操作access实例解析 
    }
}
2、配置文件中连接字符串
3、查询及判断数据存在
    string isexistsql =  select  *   from    etlsettings   where   etlname=' + name + ';
    if (dataaccess.datatable(isexistsql).rows.count == 0)
    {}
4、创建表
     string etlcreatesql = create   table    + name +
                                      ( dano varchar not null, datime  datetime  not null, logtime  datetime  not  null,  metertype  varchar  not  null,  meterno    varchar  not  null,  qty   decimal(18,6)   not  null   );
    dataaccess.excutesql(etlcreatesql);
5、增加及删除记录
 string etlsql =  insert into  etlsettings values (' + name + ',' + name + ', + 1,0);
 etlsql =  delete  from    etlsettings   where    etlname=' + name + ';
6、删除表
   drop   table   test
7、access里插入时间需要“#xxxxxxxx#”这样。
   dataaccess.excutesql(insert  into   + etlname +   (dano,datime,logtime,metertype,meterno,qty,status)   values   ('+model.dano+',#+model.datime+#,#+model.logtime+#,'+model.metertype+','+model.meterno+',+model.qty+,0));
8、access的连接数
access是允许同时有255个打开的连接,注意是打开,打开并不表示就一定在执行查询。如果要执行查询,那是另外的事,和理论支持“255个并发连接”不冲突。
access的连接是串行执行,没有并行执行模式。
   
 
   