尝试在Unity

时间:2017-05-27 14:12:51

标签: c# oracle unity3d

我几天来一直处理一个问题,没有取得任何进展。我想在Unity上使用Oracle.DataAccess(C#的Oracle数据库访问)来进行辅助项目。我安装了Oracle客户端以及数据库(工作正常),在Assets文件夹中放置.Net 2.x的Oracle.DataAccess.dll文件(只有一个Unity识别)并开始编码。这是我在类中处理的应该处理所有数据库通信的东西:

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);
        }
    }
}

尝试使用Open()时,它给了我这个例外:

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)

我仍然无法弄清楚它来自哪里。连接字符串是正确的:在构造OracleConnection对象期间发生异常(我通过在创建对象后分配连接字符串来确保它)。 DLL是64位,编辑器是64位,Oracle客户端也是64位。数据库工作正常(设法在SQL开发人员上连接到它),所以坦率地说我只是没有线索。这件事发生过吗?

提前感谢救援我的人:D

1 个答案:

答案 0 :(得分:0)

您的连接字符串似乎有误。

  1. “HOST =”之后的“+”字符不应该在那里
  2. 最后遗漏的括号
  3. 尝试使用以下内容替换该行:

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