个人编写的一个简单的DB类

Posted on 2010-11-22 14:16  孤∮帆  阅读(192)  评论(0编辑  收藏  举报

这个是我写的一个比较初级的DB类,主要是把对数据库的一些操作封装起来
public class DB
{
    OleDbConnection conn;
    OleDbCommand cmd ;
   
    public DB()
    {    
        conn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(@"~/App_Data/sj.mdb"));
        cmd = new OleDbCommand();
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

 

    public OleDbConnection open()
    {
      
        if (conn.State == ConnectionState.Closed)
            conn.Open();
        return conn;
    }

    public void close()
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();

    }

    public DataSet getDataSet(string strOleDb)
    {
        open();
        OleDbDataAdapter dr = new OleDbDataAdapter(strOleDb, conn);
        DataSet ds = new DataSet();
        dr.Fill(ds);
        return ds;
        close();
    }

/*    public OleDbDataReader getDataReader(string strOleDb)
    {
        cmd.Connection = open();
        cmd.CommandText = strOleDb;
        OleDbDataReader sdr = cmd.ExecuteReader();
        return sdr;
        close();

    }*/

    public bool execute(string strOleDb)
    {
        // bool flag=false;
        cmd.Connection = open();
        cmd.CommandText = strOleDb;
        int a= cmd.ExecuteNonQuery();
        close();
       /* if (a > 0)
        {
            flag = true;
        }
        return flag;
        */

        return a > 0 ? true : false;
      
   
    }

 

    public string ExecuteScalar(string strOleDb)
    {
        cmd.Connection = open();
        cmd.CommandText = strOleDb;
        string result = cmd.ExecuteScalar().ToString();
        close();
        return result;
    }


}

Copyright © 2024 孤∮帆
Powered by .NET 8.0 on Kubernetes