首页 > 编程 > .NET > 正文

在ASP.NET(C#)中使用Oralce Object For OLE 2.3连接Oracle 8

2024-07-10 13:05:15
字体:
来源:转载
供稿:网友
 

1、先在web应用程序项目中添加引用:interop.oracleinprocserver

2、web.config的配置:

<configuration>
  <appsettings>
 
 <!--数据库连接信息配置-->
 <add key="data source" value="db" />
 <add key="user id" value="user" />
 <add key="password" value="000000" />
 <add key="max pool size" value="100" />
 <add key="min pool size" value="0" />
 <add key="pooling" value="true" />
 
  </appsettings>

……

3、global.asax中创建oraclesession对象,代码如下:

using system;
using system.collections;
using system.componentmodel;
using system.web;
using system.web.sessionstate;

namespace sparepart
{
 /// <summary>
 /// global 的摘要说明。
 /// </summary>
 public class global : system.web.httpapplication
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private system.componentmodel.icontainer components = null;
  static public oracleinprocserver._orasessionclassclass orasession;
  static public oracleinprocserver.oradatabase oradatabase;

  public global()
  {
   initializecomponent();
  } 
  
  protected void application_start(object sender, eventargs e)
  {
   orasession=new oracleinprocserver._orasessionclassclass();
   orasession.createdatabasepool(convert.toint32(system.configuration.configurationsettings.appsettings["min pool size"]),convert.toint32(system.configuration.configurationsettings.appsettings["max pool size"]),30,system.configuration.configurationsettings.appsettings["data source"],system.configuration.configurationsettings.appsettings["user id"]+"/"+system.configuration.configurationsettings.appsettings["password"],0);

  }
 
  protected void session_start(object sender, eventargs e)
  {
   session["oradatabase"] =(oracleinprocserver.oradatabase)orasession.get_getdatabasefrompool(30);
  }

  protected void application_beginrequest(object sender, eventargs e)
  {

  }

  protected void application_endrequest(object sender, eventargs e)
  {

  }

  protected void application_authenticaterequest(object sender, eventargs e)
  {

  }

  protected void application_error(object sender, eventargs e)
  {
   
  }

  protected void session_end(object sender, eventargs e)
  {
   oracleinprocserver.oradatabase oradatabase=(oracleinprocserver.oradatabase)session["oradatabase"];
   oradatabase.close();

   session["oradatabase"]=null;
  }

  protected void application_end(object sender, eventargs e)
  {
   
  }
   
  #region web 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {   
   this.components = new system.componentmodel.container();
  }
  #endregion
 }
}

4、简单应用(default.aspx):

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace sparepart
{
 /// <summary>
 /// webform1 的摘要说明。
 /// </summary>
 public class default : system.web.ui.page
 {
  private void page_load(object sender, system.eventargs e)
  {
   // 在此处放置用户代码以初始化页面
   oracleinprocserver.oradatabase oradatabase=(oracleinprocserver.oradatabase)session["oradatabase"];
   oracleinprocserver.oradynaset orars;
   oracleinprocserver.orafields orafs;
   oracleinprocserver.orafield oraf;

   orars=(oracleinprocserver.oradynaset)oradatabase.get_createdynaset("select * from test",0);
   oradatabase.executesql("insert into test(username,password) values ('你们','密码')");
   response.write("<table border=1>");
   while (!orars.eof)
      {
     orafs=(oracleinprocserver.orafields)orars.fields;
     oraf=(oracleinprocserver.orafield)orafs["username"];

     response.write("<tr><td>姓名</td><td>");
     response.write(oraf.value);

     response.write("</td><td>密码</td><td>");
     oraf=(oracleinprocserver.orafield)orafs["password"];

     response.write(oraf.value);
     response.write("</td></tr>");
     orars.movenext();
      }
   response.write("</table>");
   orars.close();
   
  }

  #region web 窗体设计器生成的代码
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 该调用是 asp.net web 窗体设计器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {   
   this.load += new system.eventhandler(this.page_load);

  }
  #endregion
 }
}


,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表