日期:2011-06-01  浏览次数:20444 次

//访问数据库是每个程序员都想简化的工作,通过组件来完成,新手也变高手...

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.OracleClient;

namespace NetAdo
{
    public class NetDB
    {
        private string ConnStr="";
  private OracleConnection OraConn;

  public NetDB()
  {
            ConnStr = ConfigurationManager.AppSettings["Oracle9iConnStr"];
   this.OraConn=new OracleConnection();
   if(ConnStr==null)
   {
     throw new Exception("ConnectionString is Null");
   }
   this.OraConn.ConnectionString=ConnStr;
  }

  public string ConnString
  {
   get
   {
    return ConnStr;
   }
  }


  private void ConnectionPrepare(bool ifBegin)
  {
   //检查连接字符串
   if(OraConn.ConnectionString==null)
   {
    throw new Exception("OleDbConnection's ConnectionString is null,execute Init()");
               
   }
   //根据参数执行相关操作
   if(ifBegin==true)
   {
    if(OraConn.State==ConnectionState.Closed)
    {
     OraConn.Open();
    }
   }
   else
   {
    if(OraConn.State==ConnectionState.Open)
    {
     OraConn.Close();
    }
   }
  }


  public DataSet RunSqlReturnDS(string sqlString)
  {
   DataSet ds=new DataSet();
   try
   {
    ConnectionPrepare(true);
    OracleCommand Cmd=OraConn.CreateCommand();
    Cmd.CommandText=sqlString;
    OracleDataAdapter adapter=new OracleDataAdapter(Cmd);
    adapter.Fill(ds);
   }
   catch(Exception ex)
   {
    throw new Exception(ex.Message);           
   }
   finally
   {
    ConnectionPrepare(false);
   }
   return ds;
  }

  public int RunSql(string sqlString)
  {
   int RowCount=0;
   try
   {
    ConnectionPrepare(true);
    OracleCommand Cmd=OraConn.CreateCommand();
    Cmd.CommandText=sqlString;
    RowCount=(int)Cmd.ExecuteNonQuery();
   }
   catch(Exception ex)
   {
    throw new Exception(ex.Message);           
   }
   finally
   {
    ConnectionPrepare(false);
   }
   return RowCount;
  }
    }
}
 

//页面调用
using NetAdo;

public partial class _