somecold

Playing by heart,never too late

导航

初学者专用超轻量级数据库访问类

用.net时间不是太久,感觉直接用.net提供的数据库访问对象不方便,老是要不停的new,然后还要close,很多重复代码,于是动手写了一个简单的数据访问类,尤其适合于小型的net应用程序。此类主要适于使用sql语句进行数据操作,没有封装DataSet。代码如下(使用oledb数据访问对象):
 public class DB
 {
  System.Data.OleDb.OleDbCommand command=null;
  System.Data.OleDb.OleDbConnection connection=null;
  System.Data.OleDb.OleDbDataAdapter dataadapter=null;
  System.Data.OleDb.OleDbCommandBuilder commandbuilder=null;
  System.Data.OleDb.OleDbDataReader reader=null;
  System.Data.OleDb.OleDbTransaction transaction=null;

  public string ErrorInfo="";
  public bool hasError=false;

  public System.Data.OleDb.OleDbConnection Connection
  {
       get
       {
            if(connection==null)
            {
                 try
                 {
                      connection=new System.Data.OleDb.OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["dbconnstring"]);
                      connection.Open();
                 }
                 catch(Exception ee)
                 {
                      ErrorInfo=ee.Message;
                      hasError=true;
                 }
            }
            else
            {
                 try
                 {
                      connection.Open();
                 }
                 catch(Exception ee)
                 {
                      ErrorInfo=ee.Message;
                      hasError=true;
                 }
            }
            return connection;
       }
  }
  public System.Data.OleDb.OleDbCommand Command
  {
       get
       {
            if(command==null)
            {
                 try
                 {
                      command=new System.Data.OleDb.OleDbCommand("",this.Connection);
                 }
                 catch(Exception ee)
                 {
                      ErrorInfo=ee.Message;
                      hasError=true;
                 }
            }

            return command;
       }
  }
  
  public System.Data.OleDb.OleDbDataAdapter DataAdapter
  {
       get
       {
            if(dataadapter==null)
            {
                 try
                 {
                      dataadapter=new System.Data.OleDb.OleDbDataAdapter(this.Command);
                      commandbuilder=new System.Data.OleDb.OleDbCommandBuilder(dataadapter);
                 }
                 catch(Exception ee)
                 {
                      ErrorInfo=ee.Message;
                      hasError=true;
                 }
            }

            return dataadapter;
       }
  }

  public System.Data.OleDb.OleDbCommandBuilder CommandBuilder
  {
           get
           {
                return commandbuilder;
           }
  }

  public System.Data.OleDb.OleDbDataReader Reader
  {
       get
       {
            try
            {
                 if(reader==null)
                      reader=this.Command.ExecuteReader();
                 if(reader.IsClosed)
                      reader=this.Command.ExecuteReader();
            }
            catch(Exception ee)
            {
                 ErrorInfo=ee.Message;
                 hasError=true;
            }
            return reader;
       }
  }

  public System.Data.OleDb.OleDbTransaction Transaction
  {
       get
       {
            try
            {
                 if(transaction==null)
                  transaction=this.Connection.BeginTransaction();
            }
            catch(Exception ee)
            {
                 ErrorInfo=ee.Message;
                 hasError=true;
            }

            return transaction;
       }
  }

  public DB()
  {
  }

  public DB(string connstr)
  {
       try
       {
            connection=new System.Data.OleDb.OleDbConnection(connstr);
       }
       catch(Exception ee)
       {
            ErrorInfo=ee.Message;
            hasError=true;
       }
  }

  ~DB()
  {
       try
       {
            connection.Close();
       }
       catch
       {
       }
  }

  public void CloseConnection()
  {
       try
       {
            connection.Close();
       }
       catch
       {
       }
    }
 }

如果是在asp.ne中使用,则可以在web.config中加一个appsettings项:dbconnstring,直接DB db=new DB()创建就可使用,不是则使用带参数的构造函数传递连接字符串。
实际查询例子:
DB db=new DB();
db.Command.CommandText="select title,content from news where id=1";
if(db.Reader.())
{
    string title=db.Reader.getString(0);
.............
}

或者使用table或dataset:
    db.DataAdapter.fill(ds);

若要使用事务这样:
try
{
    db.Command.Transaction=db.Transaction;
    ......    数据更新操作
    db.Transaction.Commit();
}
catch
{
    db.Transaction.Rollback();
}

posted on 2005-08-29 12:00  老家伙来挨踢  阅读(740)  评论(1编辑  收藏  举报