.net C#使用Wallet连接到Oracle数据库

r6hnlfcb  于 2023-03-20  发布在  .NET
关注(0)|答案(3)|浏览(158)

我正在构建一个连接到多个Oracle数据库的应用程序。连接到内部网络上的数据库没有问题,但我需要连接到托管在Oracle云中的数据库。在SQL Developer中,连接是使用Oracle Wallet进行的,但我完全不知道如何在C#中使用Wallet。如果能提供一个示例,我将非常感激。

****编辑

我至少让它识别了我的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();
        }
    }
}

并将此添加到我的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 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

我已经尝试过用我能想到的所有方法更新app.config的WALLET_LOCATION条目,以及wallet中的sqlnet.ora文件(我认为这是一个转移注意力的问题,因为app.config似乎表示要覆盖它),但我无法克服这个错误。即使是一个指针,让我如何看到错误中引用的“String walletFile”变量的值,我也会很感激。

enxuqcxy

enxuqcxy1#

这里是examples

托管程序集/驱动程序

1.对于托管程序集,您有两个选项:
第一种选择:

  • 将www.example.com中的2个文件(sqlnet.ora、tnsname.ora)Wallet.zip放入可执行文件路径(bin/Debug)
  • 编辑sqlnet.ora并更改'DIRECTORY'变量
WALLET_LOCATION=(SOURCE=(METHOD=file)(METHOD_DATA=(DIRECTORY=<PATH_TO_WALLET_DIRECTORY>)))

第二种选择:

  • 添加XML到你的应用程序配置
<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>

非托管程序集/驱动程序

  • 将www.example.com中的2个文件(sqlnet.ora、tnsname.ora)Wallet.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);
}
s4chpxco

s4chpxco2#

当钱包中使用的别名在tnsnames.ora文件中没有匹配的条目时,我也遇到过这个问题。
值得检查的是,数据源mytns确实存在于tnsnames.ora中,并且在您的钱包中,条目的名称与tns名称匹配,因此在本例中为mytns

wdebmtf2

wdebmtf23#

使用Oracle Wallet作为凭据连接到Oracle:

C#

//Install Oracle.ManagedDataAccess.Core NuGet Package  
OracleConnection conn = new OracleConnection("Data Source=SCOTT; User ID=/");
conn.Open();

电话号码.ora

SCOTT =
(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =
(PROTOCOL=TCP)(HOST = 192.168.1.100)(PORT = 1521)
))(CONNECT_DATA = (SERVICE_NAME = MY_SERVICE)))

**数据库名称

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

相关问题