国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大丰市| 江山市| 忻州市| 铜陵市| 宜章县| 通江县| 衡南县| 霸州市| 茂名市| 内黄县| 南川市| 陇西县| 兴隆县| 阿拉善右旗| 双城市| 山东| 简阳市| 金华市| 禄丰县| 师宗县| 双牌县| 安多县| 阿图什市| 梅州市| 辉县市| 嘉禾县| 瑞安市| 名山县| 十堰市| 即墨市| 临沂市| 高平市| 夏河县| 长乐市| 天祝| 大足县| 桑植县| 新邵县| 明溪县| 磴口县| 金塔县|