//訪問數據庫是每個程序員都想簡化的工作,通過組件來完成,新手也變高手...
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 _default : system.web.ui.page 
{
    netdb nd = new netdb();
    protected void page_load(object sender, eventargs e)
    {
        try
        {
            response.write(nd.connstring.tostring() + "<br>");
            dataset objectset = new dataset();
            objectset = nd.runsqlreturnds("select * from tblszman where rownum<=10");
            response.write(objectset.tables[0].rows.count.tostring() + "<br>");
            int s = nd.runsql("drop table temptu");
            response.write(s.tostring() + "<br>");
        }
        catch (exception ex)
        {
            response.write("錯誤信息:" + ex.message);
            response.end();
        }  
    }
}
新聞熱點
疑難解答
圖片精選