仿petshop的一个数据访问层的类

类别:Asp 点击:0 评论:0 推荐:

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace yueying.Components
{
 /// <summary>
 /// DBClass 的摘要说明。
 /// </summary>
 public class DBClass
 {
        protected static string strConn = ConfigurationSettings.AppSettings["strConn"];
  public DBClass()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
        
  //功能:执行sql语句,返回bool
  internal bool ExecuteSql(string strSQL)
  {
   bool success=false;
   OleDbConnection conn = new OleDbConnection(strConn);   
   OleDbCommand myCmd = new OleDbCommand(strSQL,conn);
   try
   {
    conn.Open();
    myCmd.ExecuteNonQuery();
    success=true;
   }
   catch(System.Data.OleDb.OleDbException e)
   {    
    throw new Exception(e.Message);
   }
   finally
   {
    myCmd.Dispose();
    conn.Close();
   }
   return success;

  }

  //功能:执行sql语句,如果有记录的话返回1,否则返回0
  internal int ExecuteSqlDr(string strSQL)
  {
   OleDbConnection myCn = new OleDbConnection(strConn);   
   OleDbCommand myCmd = new OleDbCommand(strSQL,myCn);
   
   try
   {
    myCn.Open();    
    OleDbDataReader myDr = myCmd.ExecuteReader();
    
    if(myDr.Read())
    {
     return 1;
    } 
    else
    {
     return 0;
    }
   }
   catch(System.Data.OleDb.OleDbException e)
   {        
    throw new Exception(e.Message);
   }
   finally
   {
    myCmd.Dispose();
    myCn.Close();
   }
  }

  //功能:执行SQL语句,返回DataSet
  internal  DataSet ExecuteSqlDs(string strSQL ,string tablename)
  {
   OleDbConnection myCn = new OleDbConnection(strConn); 
   
   try
   {
    myCn.Open();
    OleDbDataAdapter myDa = new OleDbDataAdapter(strSQL,myCn);
    DataSet ds = new DataSet();
    myDa.Fill(ds,tablename);
    return ds;
   }
   catch(System.Data.OleDb.OleDbException e)
   {    
    throw new Exception(e.Message);
   }
   finally
   {
    myCn.Close();
   }
  }

  //执行SQL语句,返回DataReader
  internal OleDbDataReader ExecuteSqlDataReader(string strSQL)
  {
   OleDbConnection myCn = new OleDbConnection(strConn);   
   OleDbCommand myCmd = new OleDbCommand(strSQL,myCn);
   try
   {
    myCn.Open();
    return myCmd.ExecuteReader(CommandBehavior.CloseConnection); 
   }
   catch
   {
    myCn.Close();
    throw;
   }
  }

  //执行SQL语句,返回执行cmd的Scalar方法的值
  internal  int  ExecuteSqlScalar(string strSQL)
  {
   OleDbConnection myCn = new OleDbConnection(strConn);   
   OleDbCommand myCmd = new OleDbCommand(strSQL,myCn);
   try
   {
    myCn.Open();
    object r = myCmd.ExecuteScalar();
    if(Object.Equals(r,null))
    {
     throw new Exception("value unavailable!");
    }
    else
    {
     return (int)r;
    }    
   }
   catch(System.Data.OleDb.OleDbException e)
   {    
    throw new Exception(e.Message);
   }
   finally
   {
    myCmd.Dispose();
    myCn.Close();
   }
  } 

  internal object ExecuteSql4ValueEx(string strSQL)
  {
   OleDbConnection myCn = new OleDbConnection(strConn);   
   OleDbCommand myCmd = new OleDbCommand(strSQL,myCn);
   try
   {
    myCn.Open();
    object r = myCmd.ExecuteScalar();
    if(Object.Equals(r,null))
    {
     throw new Exception("object unavailable!");
    }
    else
    {
     return r;
    }    
   }
   catch(System.Data.OleDb.OleDbException e)
   {    
    throw new Exception(e.Message);
   }
   finally
   {
    myCmd.Dispose();
    myCn.Close();
   }
  }

 }
}

本文地址:http://com.8s8s.com/it/it7499.htm