在ASP.NET(C#)中使用Oralce Object For OLE 2.3串連Oracle 8.0.5資料庫!

來源:互聯網
上載者:User
asp.net|object|oracle|資料|資料庫  

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 Form設計器產生的程式碼
  /// <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 Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.