使用的DLL:Oracle.ManagedDataAccess
Bug:OracleInternal.Common.ProviderConfig的类型初始值设定项引发异常
App.config的更改才是关键
关键应该是这个:
oracle.manageddataaccess默认情况下只支持direct connection,
如果要支持tns name,即ez connection,需要在app.config或者web.config里添加如下设置
安装后的App.config:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/> </configSections> <system.data> <DbProviderFactories> <remove invariant="Oracle.ManagedDataAccess.Client"/> <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/> </DbProviderFactories> </system.data> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <publisherPolicy apply="no"/> <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/> <bindingRedirect oldVersion="4.122.0.0 - 4.65535.65535.65535" newVersion="4.122.18.3"/> </dependentAssembly> </assemblyBinding> </runtime> <oracle.manageddataaccess.client> <version number="*"> <dataSources> <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/> </dataSources> </version> </oracle.manageddataaccess.client> </configuration>
更改后的App.config:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.data> <DbProviderFactories> <remove invariant="Oracle.ManagedDataAccess.Client"/> <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/> </DbProviderFactories> </system.data> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <publisherPolicy apply="no"/> <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/> <bindingRedirect oldVersion="4.122.0.0 - 4.65535.65535.65535" newVersion="4.122.18.3"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
去掉的节点 如下:
<configSections> <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342"/> </configSections>
<oracle.manageddataaccess.client> <version number="*"> <dataSources> <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/> </dataSources> </version> </oracle.manageddataaccess.client>
代码:
OracleHelper
public class OracleHelper1 { public static OracleCommand cmd = null; public static OracleConnection conn = null; public static string connstr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.2.98)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=JJJT)));User Id=CMSP;Password=Oracle1"; public static OracleConnection init() { try { conn = new OracleConnection(connstr); if (conn.State != ConnectionState.Open) { conn.Open(); } } catch (Exception e) { throw new Exception(e.Message.ToString()); } return conn; } public static DataSet GetDataSet(string sqlstr) { DataSet set = new DataSet(); try { init(); OracleDataAdapter adp = new OracleDataAdapter(sqlstr, conn); adp.Fill(set); conn.Close(); } catch (Exception e) { throw new Exception(e.Message.ToString()); } return set; } public static int ExecuteNonQuery(string cmdText) { int count; try { init(); cmd = new OracleCommand(cmdText, conn); count = cmd.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { throw new Exception(ex.Message.ToString()); } return count; } }
CS代码
DataTable dt1 = OracleHelper1.GetDataSet("select * from TB_PROJECTS").Tables[0];