仿照以前收集的一個經典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
}
新聞熱點
疑難解答
圖片精選