赞
踩
要连接Oracle需要先安装Oracle驱动ODTwithODAC121024,具体名称为ODAC with Oracle Developer Tools for Visual Studio,下载地址为https://www.oracle.com/database/technologies/dotnet-odacdev-downloads.html,
下载后安装比较慢,安装完成后找到product\12.1.0\client_1\odp.net\bin\4路径里面的OraProvCfg.exe文件双击运行后会在环境变量自动增加Oracle驱动的路径:
运行完成后会右键 此电脑(或我的电脑)-》高级系统设置-》环境变量-》找到Path:
双击Path可以看到刚才运行OraProvCfg.exe新增的环境变量配置
如果使用.net2.0版本则需要找到product\12.1.0\client_1\odp.net\bin\2.x路径,其他的配置同上面的4.0配置一样。
安装驱动后会在C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config目录的machine.config文件夹自动配置:
<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<section name="oracle.unmanageddataaccess.client" type="OracleInternal.Common.CustomSectionHandler, Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<section name="oracle.dataaccess.client" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
还有下面部分,如果没有就需要自己手动添加:
<membership>
<providers>
<add name="OracleMembershipProvider" type="Oracle.Web.Security.OracleMembershipProvider, Oracle.Web, Version=4.121.2.1, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionStringName="OraAspNetConString" applicationName="" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
</membership>
<profile>
<providers>
<add name="OracleProfileProvider" type="Oracle.Web.Profile.OracleProfileProvider, Oracle.Web, Version=4.121.2.1, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionStringName="OraAspNetConString" applicationName="" />
</providers>
</profile>
<roleManager>
<providers>
<add name="OracleRoleProvider" type="Oracle.Web.Security.OracleRoleProvider, Oracle.Web, Version=4.121.2.1, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionStringName="OraAspNetConString" applicationName="" />
<add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
找到product\12.1.0\client_1\odp.net\bin\4目录下的Oracle.DataAccess.dll文件复制到项目里面并且引入(.net2.0在D:\Soft\Oracle\product\12.1.0\client_1\odp.net\bin\2.x)
web.config的connectionStrings节点里面增加Oracle数据的连接
<add name ="OracleDB" connectionString ="Data Source=192.168.1.1/数据库名(orcl);User ID=sa;Password=123" providerName="Oracle.DataAccess.Client" />
安装Oracle驱动后如果获取数据提示报错:未将对象引入到实例的解决办法:
oracle驱动的安装路径product\12.1.0\client_1中找到oraons.dll文件,然后复制到product\12.1.0\client_1\bin目录中即可。
using (OracleConnection conn = new OracleConnection(System.Configuration.ConfigurationManager.AppSettings["OracleDB"]))
{
conn.Open();
string sql = "select * from 库.表";
using (OracleCommand comm = new OracleCommand(sql, conn))
{
using (OracleDataReader rdr = comm.ExecuteReader())
{
while (rdr.Read())
{
Console.WriteLine("UserName:" + rdr.GetString(0) + ",UserId:" + rdr.GetInt32(1) + ",点数:" + rdr.GetInt32(2) + "," + rdr.GetDateTime(3));
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。