ODP.NET连接异常

时间:2014-08-26 14:30:31

标签: c# oracle odp.net

首先,我想说我对Oracle数据库一点都不熟悉,所以我的话可能选择不当,而且我对某些概念的理解可能是错误的...... 无论如何,我试图使用ODP.NET连接到Oracle 11g数据库,每次它都给我这个例外:

System.TypeInitializationException occurred
HResult=-2146233036
Message=The type initializer for 'OracleInternal.Network.AddressResolution' threw an exception.
Source=Kiwi.ServiceBase
TypeName=OracleInternal.Network.AddressResolution
StackTrace:
    at OracleInternal.ConnectionPool.PoolManager`3.CreateNewPR(Int32 reqCount, Boolean bForPoolPopulation, ConnectionString csWithDiffOrNewPwd, String instanceName)

    at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)

    at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)

    at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword)

    at Oracle.ManagedDataAccess.Client.OracleConnection.Open()

    at Kiwi.DataAccess.OracleDataService.get_DbConnection() in c:\Projects\Kiwi-Beta7-0\Kiwiweb\src\Common\ApplicationServices\DataService\OracleDataService.cs:line 28

    InnerException: System.TypeInitializationException
        HResult=-2146233036
        Message=The type initializer for 'OracleInternal.Network.LDAP' threw an exception.
        Source=Oracle.ManagedDataAccess
        TypeName=OracleInternal.Network.LDAP
        StackTrace:
            at OracleInternal.Network.LDAP..ctor()
            at OracleInternal.Network.AddressResolution..cctor()
        InnerException: System.NullReferenceException
            HResult=-2147467261
            Message=Object reference not set to an instance of an object.
            Source=Oracle.ManagedDataAccess
            StackTrace:
                at OracleInternal.Network.LDAP._LDAP(Hashtable dsMap)
                at OracleInternal.Network.LDAP..cctor()
            InnerException:

来自:

private System.Data.IDbConnection _dbConnection;
public override IDbConnection DbConnection
{
    get
    {
        if (_dbConnection.State == ConnectionState.Closed)
            _dbConnection.Open();        // Crash from HERE
        return _dbConnection;
    }
}

编辑其他信息:我也尝试在没有tnsname.ora文件的情况下进行连接。这是在数据库所在的同一服务器上运行的Windows服务。

我尝试了以下连接字符串,第一个在调试中工作(并且是由应用程序构造的连接字符串):

Data Source=demosyr20140329:1521/demosyr;User ID=SEI;password=manager;Pooling = False;

Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)       (HOST=DEMOSYR20140329)(PORT=1521))(CONNECT_DATA=(SID=DEMOSYR)));User Id=system;Password=manager;

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=DEMOSYR20140329)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=DEMOSYR)));

Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=DEMOSYR20140329)(PORT=1521))(CONNECT_DATA=(SID=DEMOSYR)));  User Id=system;Password=manager;

User Id=system;Password=manager;Data Source=oracle

Data Source=system/manager@//DEMOSYR20140329:1521/DEMOSYR;

有什么想法吗?

3 个答案:

答案 0 :(得分:7)

这家伙帮我解决了我的问题 https://pravsdatums.wordpress.com/2013/12/16/ocac-12c-and-visual-studio-developer-tools/#comment-1

解决方案是从sql.ora文件中的NAMES.DIRECTORY_PATH中删除LDAP。此文件位于客户端主目录中(此路径可在HLM / SOFTWARE / ORACLE下的注册表中的某个密钥上找到)。所以文件的内容来自

SQLNET.AUTHENTICATION_SERVICES= (none)

NAMES.DIRECTORY_PATH= (LDAP, TNSNAMES, EZCONNECT)

SQLNET.AUTHENTICATION_SERVICES= (none)

NAMES.DIRECTORY_PATH= (EZCONNECT, TNSNAMES)

现在它有效!我不知道为什么它在我的开发机器上工作(可能是因为没有安装Oracle)。

希望它可以帮助别人!

答案 1 :(得分:0)

除了如上所述修复sqlnet.ora之外,还请检查TNS_ADMIN环境变量是否指向Oracle客户端的正确network \ admin路径。一旦这是正确的,重新启动,一切都应该很好!

答案 2 :(得分:-1)

我的猜测是连接字符串是一个问题。一些建议:

  1. 您可以使用SqlPlus或SqlDeveloper连接到数据库 本地?
  2. 如果(1)的答案为真,那么我会尝试修改 _dbConnection.ConnectionString直接在代码中直到我做对了。如果您使用tnsnames.ora进行sqlplus连接,我 将来自那里的代码放在连接字符串中 并验证连接是否完整。我知道这不是你的最终目标,但你必须从某个地方开始。
  3. 如果对(1)的回答是错误的,我会在尝试通过ODP.Net进行连接之前首先获得该连接。