在ASP.NET(C#)中使用Oralce Object For OLE 2.3连接Oracle 8.0.5数据库!
在ASP.NET(C#)中使用Oralce Object For OLE 2.3连接Oracle 8.0.5数据库! 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
}
}