我正在构建一个连接到多个Oracle数据库的应用程序。 连接到内部网络上的数据库没有问题,但我需要连接到 Oracle 云上托管的数据库。 在SQL开发人员中,连接是使用Oracle钱包建立的,但我实际上不知道如何在C#中使用钱包。 一个例子将不胜感激。
****编辑
我至少让它识别我的TNS条目,但我遇到了一个我无法取得进展的问题。 这是我的代码;
using System;
using System.Data;
using System.Data.Common;
using Oracle.ManagedDataAccess.Client;
class GetSchemaSample
{
static void Main(string[] args)
{
string constr = "User Id=/; Data Source=mytns;";
string ProviderName = "Oracle.ManagedDataAccess.Client";
DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);
using (DbConnection conn = factory.CreateConnection())
{
try
{
conn.ConnectionString = constr;
conn.Open();
DataTable dtSchema = conn.GetSchema();
dtSchema.WriteXml(ProviderName + "_Schema.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
Console.ReadLine();
}
}
}
并将其添加到我的应用程序配置中;
<oracle.manageddataaccess.client>
<version number="*">
<settings>
<setting name="WALLET_LOCATION" value="c:Oracle" />
<setting name="SQLNET.WALLET_OVERRIDE" value="true" />
</settings>
</version>
</oracle.manageddataaccess.client>
我遇到的错误是;
The path is not of a legal form.
at System.IO.Path.LegacyNormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.Path.GetFullPath(String path)
at System.IO.FileSystemWatcher.StartRaisingEvents()
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.CreateSEPSFileWatcher(String walletPath, String walletFile)
at OracleInternal.ConnectionPool.OraclePoolManager.InitializeSEPSCredentials()
at OracleInternal.ConnectionPool.OraclePoolManager.Initialize(ConnectionString cs, OracleConnection con)
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.GetPM(ConnectionString cs, OracleConnection con, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword, Boolean& bAuthenticated, Boolean& newPM)
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword, OracleConnection connRefForCriteria)
at Oracle.ManagedDataAccess.Client.OracleConnection.Open()
at GetSchemaSample.Main(String[] args) in C:Userspeter.hollidaysourcereposOACTestingConnection1.cs:line 39
我已经尝试以我能想到的各种方式更新 app.config WALLET_LOCATION条目以及钱包中的 sqlnet.ora 文件(我认为这是一个红鲱鱼,因为 app.config 似乎表明覆盖了这一点(,但我无法克服这个错误。 即使是指向我如何查看错误中引用的"字符串钱包文件"变量的值的指针也将不胜感激。
这里有例子
Managed Assemby/Driver
- 使用托管组装,您有 2 个选项:
第一个选项:
- 将 2 个文件(sqlnet.ora、tnsnames.ora(从钱包.zip放入您的可执行路径(bin/Debug(
- 编辑 sqlnet.ora 并更改 'DIRECTORY' 变量
WALLET_LOCATION=(SOURCE=(METHOD=file)(METHOD_DATA=(DIRECTORY=<PATH_TO_WALLET_DIRECTORY>)))
第二种选择:
- 将 XML 添加到您的 app.config
<oracle.manageddataaccess.client>
<version number="*">
<settings>
<setting name="TNS_ADMIN" value="PATH_TO_WALLET_DIRECTORY"/>
<setting name="Wallet_Location" value="PATH_TO_WALLET_DIRECTORY"/>
</settings>
</version>
</oracle.manageddataaccess.client>
非托管组件/驱动程序
- 将 2 个文件(sqlnet.ora、tnsnames.ora(从钱包.zip放入您的可执行路径(bin/Debug(
- 编辑sqlnet.ora并更改"DIRECTORY"变量(与托管驱动程序相同,选项优先(
连接到数据库
public static OracleConnection TakeMeToTheClouds()
{
var user = "admin";
var password = "YOUR PASSWORD";
var ds = "THIS YOU FIND IN 'tnsnames.ora' FILE";
var connectionString = $"User Id={user};Password={password};Data Source={ds};";
return new OracleConnection(connectionString);
}
当钱包中使用的别名在tnsnames.ora
文件中没有匹配的条目时,我也遇到了这个问题。
值得检查的是,数据源mytns
确实存在于tnsnames.ora
中,并且在您的钱包中,条目的名称与 tns 名称匹配,因此在这种情况下mytns
.
钱包作为凭据连接到 Oracle:
C#
//Install Oracle.ManagedDataAccess.Core NuGet Package
OracleConnection conn = new OracleConnection("Data Source=SCOTT; User ID=/");
conn.Open();
tnsnames.ora
SCOTT =
(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =
(PROTOCOL=TCP)(HOST = 192.168.1.100)(PORT = 1521)
))(CONNECT_DATA = (SERVICE_NAME = MY_SERVICE)))
sqlnet.ora
wallet_location = (source = (method = file)
(method_data=(directory = C:scott_wallet))
)
SQLNET.WALLET_OVERRIDE=TRUE
钱包创建:
mkstore -wrl scott_wallet -create
mkstore -wrl scott_wallet -createCredential SCOTT SCOTT