Use Oralce Object for OLE 2.3 in asp.net (C #) to connect to the Oracle 8.0.5 database!

Source: Internet
Author: User
Tags min modify connect ole
asp.net|object|oracle| Data | database

1. Add a reference to the Web application project first: Interop.oracleinprocserver

2, the Web.config configuration:

<configuration>
<appSettings>

<!--database connection information configuration-->
<add key= "Data Source" value= "DB"/>
<add key= "User ID" value= "user"/>
<add key= "Password" value= "000000"/>
<add key= "Max Pool Size" value= "/>"
<add key= "Min Pool Size" value= "0"/>
<add key= "Pooling" value= "true"/>

</appSettings>

......

3, Global.asax Create the Oraclesession object, the code is as follows:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Web;
Using System.Web.SessionState;

A summary description of the

namespace Sparepart
{
 ///<summary>
 ///Global.
 ///</summary>
 public class Global:System.Web.HttpApplication
 {
   <summary>
  ///A required designer variable.
  ///</summary>
  private System.ComponentModel.IContainer components = null;
  static public Oracleinprocserver._orasessionclassclass orasession;
  static public oracleinprocserver.oradatabase oradatabase;

  public Global ()
  {
   initializecomponent ();
&NBSP;&NBSP;}&NBSP
  
  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)
{

}

Code generated #region the Web forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
this.components = new System.ComponentModel.Container ();
}
#endregion
}
}

4. Simple Application (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>
Summary description of the WebForm1.
</summary>
public class Default:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
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 (' You ', ' Password ')");
Response.Write ("<table border=1>");
while (! orars.eof)
{
Orafs= (oracleinprocserver.orafields) orars.fields;
oraf= (Oracleinprocserver.orafield) orafs["username"];

Response.Write ("<tr><td> name </td><td>");
Response.Write (Oraf.value);

Response.Write ("</td><td> password </td><td>");
oraf= (Oracleinprocserver.orafield) orafs["password"];

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

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.