首页 > 编程 > .NET > 正文

Access 通用数据访问类(asp.net 2.0 c#)

2024-07-10 13:04:44
字体:
来源:转载
供稿:网友

仿照以前收集的一个经典sql server数据访问类,稍做修改。
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.oledb;


/// <summary>
/// dataaccess 的摘要说明
/// </summary>
public class dataaccess
{
    protected static oledbconnection conn = new oledbconnection();
    protected static oledbcommand comm = new oledbcommand();
 public dataaccess()
 {
  //init
 }
    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); }

        }
      
    }//打开数据库
 
    private static void closeconnection()
    {
        if (conn.state == connectionstate.open)
        {
            conn.close();
            conn.dispose();
            comm.dispose();
        }
    }//关闭数据库

    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语句

    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();
            }
            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

    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

    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

    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

}

上一篇:Windows.NET Server: IIS 6.0

下一篇:ASP.NET Cache

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表