老系统中用的是Oracle10g,站点服务器上安装了Oracle10g的客户端。
现在数据库换成了oracle12c,站点服务器上不在安装12c的客户端,用instantclient_12_1来链接12c数据库。
文章描述了配置过程
准备工作
1:下载 instantclient-basic-windows.x64-12.1.0.2.0.zip 并解压缩到一个稳定的目录
解压缩后
2: 从Oracle12c的服务端程序安装目录里拷贝两个文件出来 OraOps12.dll 和 Oracle.DataAccess.dll
OraOps12.dll
image.png
3:将OraOps12.dll 和 Oracle.DataAccess.dll 两个文件复制到 instantclient_12_1 下
image.png
站点修改
1:web.config中的配置,注意每一项都要有,配置了DllPath 就不需要在系统环境变量里添加Path路径了
<configSections>
<section name="oracle.dataaccess.client" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<connectionStrings>
<add name="MyConnectionOne" providerName="Oracle.DataAccess.Client" connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXXXXX)(PORT=1521)))(CONNECT_DATA=(sid =orcl)));User Id=XXXXXX;Password=XXXXXX"/>
</connectionStrings>
<system.data>
<DbProviderFactories>
<clear />
<add name="ODP.NET, Unmanaged Driver" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET, Unmanaged Driver" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>
<oracle.dataaccess.client>
<settings>
<add name="DllPath" value="D:\09Dev\instantclient_12_1\"/>
</settings>
</oracle.dataaccess.client>
2:站点程序上添加Oracle.DataAccess.dll引用,确认版本一致
image.png
3:测试代码 前端页面,default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<br />
<asp:Literal ID="idlist" runat="server"></asp:Literal>
</asp:Content>
4:测试代码 default.aspx后台
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.Common;
namespace WebApplication1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strSql = "select * from testtest";
using (IDataReader reader = GetDataReader(strSql))
{
while (reader.Read())
{
string id = reader[1].ToString();
idlist.Text = idlist.Text + id + "<br/>";
}
}
}
public DbConnection GetConnection()
{
string providerName = "Oracle.DataAccess.Client";
string strConnection = ConfigurationManager.ConnectionStrings["MyConnectionOne"].ToString();
System.Data.Common.DbProviderFactory dbFactory = System.Data.Common.DbProviderFactories.GetFactory(providerName);
DbConnection conn = dbFactory.CreateConnection();
conn.ConnectionString = strConnection;
conn.Open();
return conn;
}
public IDataReader GetDataReader(string SQL)
{
var conn = GetConnection();
var cmd = conn.CreateCommand();
cmd.CommandText = SQL;
IDataReader Rs = cmd.ExecuteReader();
return Rs;
}
}
}
最终呈现
image.pngimage.png
网友评论