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
}
}