使用 Oracle.ManagedDataAccess 连接到 Oracle
Connecting to Oracle using Oracle.ManagedDataAccess
我在我的 C# (.NET 4.0) 项目中使用 Oracle.ManagedDataAccess Nuget 包版本 12.1.022。该包会自动在 app.config 文件中创建条目。我如何从该文件中读取数据源字符串才能连接到数据库?
System.Configuration.ConfigurationManager.AppSettings.Get("dataSource");
不适合我
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="oracle.manageddataaccess.client"
type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, 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.121.2.0, 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.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="MyDataSource"
descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
</configuration>
使用包 Formo,我创建了以下方法从 app.config
文件
中获取 MyDataSource
字符串
using Configuration = Formo.Configuration;
.......
/// <summary>
/// Gets the data source from app.config file
/// </summary>
/// <returns></returns>
public string GetMyDataSource()
{
dynamic config = new Configuration();
return config.MyDataSource;
}
通常,您会在标准连接字符串中引用别名:
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=MyDataSource;User Id=scott;Password=tiger;"/>
</connectionStrings>
然后您将使用标准方法检索字符串:
ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
另请记住,数据源部分中的别名是可选的。您可以将描述符直接嵌入到连接字符串中:
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)));User Id=scott;Password=tiger;"/>
</connectionStrings>
您还可以在 tnsnames.ora 文件中引用别名。默认情况下,驱动程序在 exe 文件夹中查找 tnsnames.ora,在 TNS_ADMIN 环境变量中指定的目录,或 TNS_ADMIN 配置变量:
http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId6
http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId7
您可以在连接字符串中引用已经定义的数据源
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="MyDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
例如,在这种特定情况下:
public class OracleDBManager
{
private OracleConnection _con;
private const string connectionString = "User Id={0};Password={1};Data Source=MyDataSource;";
private const string OracleDBUser = "exampleUser";
private const string OracleDBPassword = "examplePassword";
public OracleDBManager()
{
InitializeDBConnection();
}
~OracleDBManager()
{
if (_con != null)
{
_con.Close();
_con.Dispose();
_con = null;
}
}
private void InitializeDBConnection()
{
_con = new OracleConnection();
_con.ConnectionString = string.Format(connectionString, OracleDBUser, OracleDBPassword);
_con.Open();
}
}
您可以创建一个单独的 class 文件返回连接字符串,就像这样 -
public class OracleDbConnection
{
public static OracleConnection GetConnection()
{
const string connectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=PROD))); User Id=userId;Password=password;";
var connection=new OracleConnection(connectionString);
return connection;
}
}
然后在需要访问oracle db的地方可以这样调用-
var oracleConnection = OracleDbConnection.GetConnection();
oracleConnection.Open();
我在我的 C# (.NET 4.0) 项目中使用 Oracle.ManagedDataAccess Nuget 包版本 12.1.022。该包会自动在 app.config 文件中创建条目。我如何从该文件中读取数据源字符串才能连接到数据库?
System.Configuration.ConfigurationManager.AppSettings.Get("dataSource");
不适合我
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="oracle.manageddataaccess.client"
type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, 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.121.2.0, 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.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="MyDataSource"
descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
</configuration>
使用包 Formo,我创建了以下方法从 app.config
文件
MyDataSource
字符串
using Configuration = Formo.Configuration;
.......
/// <summary>
/// Gets the data source from app.config file
/// </summary>
/// <returns></returns>
public string GetMyDataSource()
{
dynamic config = new Configuration();
return config.MyDataSource;
}
通常,您会在标准连接字符串中引用别名:
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=MyDataSource;User Id=scott;Password=tiger;"/>
</connectionStrings>
然后您将使用标准方法检索字符串:
ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
另请记住,数据源部分中的别名是可选的。您可以将描述符直接嵌入到连接字符串中:
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)));User Id=scott;Password=tiger;"/>
</connectionStrings>
您还可以在 tnsnames.ora 文件中引用别名。默认情况下,驱动程序在 exe 文件夹中查找 tnsnames.ora,在 TNS_ADMIN 环境变量中指定的目录,或 TNS_ADMIN 配置变量:
http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId6 http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId7
您可以在连接字符串中引用已经定义的数据源
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="MyDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
例如,在这种特定情况下:
public class OracleDBManager
{
private OracleConnection _con;
private const string connectionString = "User Id={0};Password={1};Data Source=MyDataSource;";
private const string OracleDBUser = "exampleUser";
private const string OracleDBPassword = "examplePassword";
public OracleDBManager()
{
InitializeDBConnection();
}
~OracleDBManager()
{
if (_con != null)
{
_con.Close();
_con.Dispose();
_con = null;
}
}
private void InitializeDBConnection()
{
_con = new OracleConnection();
_con.ConnectionString = string.Format(connectionString, OracleDBUser, OracleDBPassword);
_con.Open();
}
}
您可以创建一个单独的 class 文件返回连接字符串,就像这样 -
public class OracleDbConnection
{
public static OracleConnection GetConnection()
{
const string connectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=PROD))); User Id=userId;Password=password;";
var connection=new OracleConnection(connectionString);
return connection;
}
}
然后在需要访问oracle db的地方可以这样调用-
var oracleConnection = OracleDbConnection.GetConnection();
oracleConnection.Open();