简体   繁体   中英

Invalid Cast Exception when trying to use Oracle.DataAccess on Unity

I have been dealing with an issue for a few days now, making no progress. I want to use Oracle.DataAccess (Oracle DB access for C#) on Unity for a side project. I installed Oracle client aswell as a database (which is working fine), put the Oracle.DataAccess.dll file for .Net 2.x (only one Unity recognises) in the Assets folder and started coding. Here's what I typed in the class supposed to handle all DB communication stuff :

using System;
using System.Collections.Generic;
using UnityEngine;

using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

public static class DBConnection
{

    //
    static string Host;
    static int Port;
    static string SID;
    static string Username;
    static string Password;
    //

    static bool Initialized = false;
    public static void Initialize(string host, int port, string sid, string username, string password)
    {
        if (Initialized) throw new Exception("DBConnection was already initialized !");

        Host = host;
        Port = port;
        SID = sid;
        Username = username;
        Password = password;
    }

    static OracleConnection Connection;
    public static void Open()
    {
        try
        {
            string connString = "user id=" + Username + ";password=" + Password + ";data source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=+" + Host + ")(Port=" + 1521 + "))(CONNECT_DATA=(SERVICE_NAME=" + SID + "))";
            using (Connection = new OracleConnection(connString))
            {
                // ...
            }
        }
        catch (OracleException e)
        {
            Debug.LogError(e.ErrorCode);
        }
    }
}

Upon trying to use Open(), it gives me this exception :

InvalidCastException: Cannot cast from source type to destination type.
Oracle.DataAccess.Client.CThreadPool..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Oracle.DataAccess.Client.CThreadPool
Oracle.DataAccess.Client.OracleInit.Initialize ()
Oracle.DataAccess.Client.OracleConnection..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Oracle.DataAccess.Client.OracleConnection
DBConnection.Open () (at Assets/Sources/Database/DBConnection.cs:37)
Game.Start () (at Assets/Sources/Game/Game.cs:10)

And I still couldn't figure out where it comes from. The connection string is correct : the exception happens during the construction of the OracleConnection object (I made sure of it by assigning the connection string after the object is created). The DLL is 64 bits, the editor is 64 bits and Oracle client is also 64 bits. The database works fine (managed to connect to it on SQL developper), so frankly I'm just out of leads. Did this ever happen to any of you ?

Thanks in advance to whoever rescues me :D

Your connection string seems to be mistaken.

  1. "+" character after "HOST=" should not be there
  2. Closing parentheses missing at the end

Try to replace the line with the following:

string connString = "user id=" + Username + ";password=" + Password + ";data source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=" + Host + ")(Port=" + 1521 + "))(CONNECT_DATA=(SERVICE_NAME=" + SID + ")))";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM