Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

ODBC Connections impossible with Unity5?

Discussion in 'Editor & General Support' started by rab, Jul 22, 2015.

  1. rab

    rab

    Joined:
    Nov 13, 2009
    Posts:
    101
    Hello Unity-Users,

    does anybody has an idea why connections to ODBC databases won't run in Unity5 (64 bit) anymore (in Unity4 it worked perfectly)?

    Another user wrote about the same problem here (trying to connect to a xls file):
    http://answers.unity3d.com/questions/923013/load-data-from-excel-with-odbc-doesnt-work-in-unit.html

    I got the same problem when I try to connect to an access database file:

    OdbcException: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified System.Data.Odbc.OdbcConnection.Open ()

    In Unity4 the code below works without errors and reads the stored data from the database. The path to the file is correct too, checked this multiple times ...

    Thanks for any help!
    Kind regards!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Data;
    5. using System.Data.Odbc;
    6.  
    7. public class Database : MonoBehaviour
    8. {
    9.  
    10.     public string text = "";
    11.  
    12.     void Start()
    13.     {
    14.         string path = Application.dataPath + "/DinnerDB.accdb";
    15.         ReadTable(path);
    16.     }
    17.  
    18.     internal void ReadTable(string filetoread)
    19.     {
    20.         string connection = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + Application.dataPath + "/DinnerDB.accdb" + ";";
    21.  
    22.         string sqlQuery = "select * from Zutaten";
    23.         OdbcConnection con = new OdbcConnection(connection);
    24.         OdbcCommand cmd = new OdbcCommand(sqlQuery, con);
    25.         DataTable dt = new DataTable("Zutaten");
    26.  
    27.         try
    28.         {
    29.             con.Open();
    30.             OdbcDataReader reader = cmd.ExecuteReader();
    31.             dt.Load(reader);
    32.             reader.Close();
    33.             con.Close();
    34.         }
    35.  
    36.         catch (Exception ex)
    37.         {
    38.             Debug.Log(ex.ToString());
    39.         }
    40.  
    41.         finally
    42.         {
    43.             if (con.State != ConnectionState.Closed)
    44.             {
    45.                 con.Close();
    46.             }
    47.             con.Dispose();
    48.         }
    49.  
    50.         if (dt.Rows.Count > 0)
    51.         {
    52.             text = dt.Rows[1][1].ToString();
    53.         }
    54.     }
    55. }
     
  2. rab

    rab

    Joined:
    Nov 13, 2009
    Posts:
    101
    Update: Just checked the 32 bit version of 5.1.2 -> working there so the problem seems to be at the 64 bit version of Unity.
     
  3. james_m_russell

    james_m_russell

    Joined:
    Jun 2, 2015
    Posts:
    25
    Going out on a limb on a possible solution (my apologies if you have already checked this).
    There are actually two separate ODBC admin tools for windows. one is 32 bit and one is 64 bit. sounds like you've made a 32 bit DSN, but not a 64 bit one.

    on a 64 bit windows install
    C:\Windows\System32\odbcad32.exe is the 64 bit version
    C:\Windows\SysWOW64\odbcad32.exe is the 32 bit version

    purely guessing, but I wonder if previous unity builds only used the 32bit DSNs, and they added 64 bit DSN support in 5.
     
  4. rab

    rab

    Joined:
    Nov 13, 2009
    Posts:
    101
    Thanks james_m_russell, you pointed at the right thing:

    odbc_bug.jpg

    The problem could be solved by installing a 64 bit driver for Access but there are some problems with older Office installations (Office 2007 is only 32 bit) and the 64 bit driver. Here is a discussion with links to the 64 bit driver:
    http://answers.microsoft.com/en-us/...database/be8c0ad4-d8fd-48e0-9026-b95d84135820

    I will simply use the 32 bit version of Unity5 to avoid the problem at Office 2007.

    Thanks!
     
  5. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    I had the same issue and this Q&A was very helpful to narrow down my problem.
    On a Windows 7 64 bit machine with Unity 64 bit, the Unity's Mono System.Data.Odbc class relies on the odbc32.dll that is located in Windows\System32 and not the one in Windows\SysWOW64. I found out the driver wasn't listed in the odbcad32.exe administration tool located in Windows\System32.
    Installing the 64 bit version of Microsoft Access Database Engine 2010 Redistributable solved my issues as I now have the correct 64 bit driver listed in the odbcad32.exe application.
    The drivers can be downloaded from here: https://www.microsoft.com/en-us/download/details.aspx?id=13255
    The driver name in all the connection strings in the code must be updated with the new driver's name (that can be seen with the odbc administration tool).
    I am now using Unity 5.3 64 bit and connecting successfully to the data source, in my case a dbf file.
    I am sure this doesn't cover all the error IM002 cases but if you have this issue, this is one thing you want to exclude anyway.
     
    Last edited: Feb 22, 2016