[英]C# connect to Oracle database using Wallet
I am building an app that connects to several Oracle databases.我正在构建一个连接到多个 Oracle 数据库的应用程序。 I have no problem connecting to databases on our internal.network, but I need to connect to one on that is hosted on the Oracle Cloud.我可以毫无问题地连接到我们内部网络上的数据库,但我需要连接到一个托管在 Oracle 云上的数据库。 In SQL Developer the connection is made using an Oracle Wallet, but I have literally no idea how to utilize the wallet in C#. An example would be really appreciated.在 SQL Developer 中,连接是使用 Oracle 钱包建立的,但我完全不知道如何在 C# 中使用钱包。非常感谢一个例子。
****Edit ****编辑
I have at least got it to recognise my TNS entries, but I have hit a problem that I can make no progress on.我至少让它能够识别我的 TNS 条目,但我遇到了一个我无法取得进展的问题。 Here is my code;这是我的代码;
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();
}
}
}
And added this to my App.config;并将其添加到我的 App.config 中;
<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 error I am encountering is;我遇到的错误是;
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:\Users\peter.holliday\source\repos\OACTesting\Connection1.cs:line 39
I have tried updating app.config WALLET_LOCATION entry in every way I can think of and also the sq.net.ora file in the wallet (I assume this is a red herring as app.config seems to indicate overriding that), but I can't get past this error.我已经尝试以我能想到的所有方式更新 app.config WALLET_LOCATION 条目以及钱包中的 sq.net.ora 文件(我认为这是一个红色鲱鱼,因为 app.config 似乎表明要覆盖它),但我可以'克服这个错误。 Even a pointer to how I can see what the value of "String walletFile" variable referred to in the error is would be appreciated.即使是指向我如何查看错误中引用的“String walletFile”变量的值的指针,也将不胜感激。
First option:第一个选项:
WALLET_LOCATION=(SOURCE=(METHOD=file)(METHOD_DATA=(DIRECTORY=<PATH_TO_WALLET_DIRECTORY>)))
Second option:第二种选择:
<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>
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);
}
I have also encountered this issue when the alias used in the wallet does not have a matching entry in the tnsnames.ora
file.当钱包中使用的别名在tnsnames.ora
文件中没有匹配条目时,我也遇到过这个问题。
It is worth checking that the datasource mytns
does indeed exist in tnsnames.ora
, and that in your wallet, the name of the entry matches the tns name, so in this case mytns
.值得检查数据源mytns
确实存在于tnsnames.ora
中,并且在您的钱包中,条目的名称与 tns 名称匹配,因此在本例中mytns
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.