1

I have created database in Microsoft access 2007 (2016 file format). And trying to connect Unity3D version (2019.4.16f1) to this database. Using the following code written in c#.

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Data;
using System.Data.Odbc;
public class DatabaseConnect : MonoBehaviour
{
public string FileName;
public string Table_Name;
public string Column_name;
public DataTable Dt;
public string text;
public Text testtext;
public void Start()
{
    FileName = "Database for Jobshop.accdb";
    Table_Name = "a- Lathe Machines";
    Column_name = "MC No";

    ReadACCDB(Application.dataPath + "/" + FileName);
}

internal void ReadACCDB(string fileToReadFrom)
{
    //string connection = "Driver = {FestoODBCTest}; Dbq = " + fileToReadFrom +"; Uid = ; Pwd = ;";
    string connection = "Driver ={MS Access Database(*.mdb, *.accdb)};" +
        " Dbq = " + fileToReadFrom + ";";
    Debug.Log("The connection string");
    Debug.Log(connection);
    string sqlQuery = "SELECT " + Column_name + " FROM " + Table_Name;
    OdbcConnection con = new OdbcConnection(connection);
    OdbcCommand cmd = new OdbcCommand(sqlQuery, con);

    try
    {
        con.Open();
        OdbcDataReader reader = cmd.ExecuteReader();
        Dt.Load(reader);
        reader.Close();
        con.Close();
    }

    catch (Exception ex)
    {
        Debug.Log("Throws an exception");
        Debug.Log(ex.ToString());
    }

    finally
    {
        if (con.State != ConnectionState.Closed)
        {
            con.Close();
        }
        con.Dispose();
    }
    if (Dt.Rows.Count > 0 && Dt.Columns.Count > 0)
    {
        Debug.Log(Dt.ToString());
        testtext.text = Dt.ToString();
    }
    else
    {
        Debug.Log("Didnt find a table");
        testtext.text = "Didnt Find a table";
    }
}
}

But I Got the following exception.

System.Data.Odbc.OdbcException (0x80131937): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified at System.Data.Odbc.OdbcConnection.HandleError (System.Data.Odbc.OdbcHandle hrHandle, System.Data.Odbc.ODBC32+RetCode retcode) [0x0000d] in <290425a50ff84a639f8c060e2d4530f6>:0 at (wrapper remoting-invoke-with-check) System.Data.Odbc.OdbcConnection.HandleError(System.Data.Odbc.OdbcHandle,System.Data.Odbc.ODBC32/RetCode) at System.Data.Odbc.OdbcConnectionHandle..ctor (System.Data.Odbc.OdbcConnection connection, System.Data.Odbc.OdbcConnectionString constr, System.Data.Odbc.OdbcEnvironmentHandle environmentHandle) [0x0004c] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.Odbc.OdbcConnectionOpen..ctor (System.Data.Odbc.OdbcConnection outerConnection, System.Data.Odbc.OdbcConnectionString connectionOptions) [0x0000c] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.Odbc.OdbcConnectionFactory.CreateConnection (System.Data.Common.DbConnectionOptions options, System.Data.Common.DbConnectionPoolKey poolKey, System.Object poolGroupProviderInfo, System.Data.ProviderBase.DbConnectionPool pool, System.Data.Common.DbConnection owningObject) [0x00000] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.ProviderBase.DbConnectionFactory.CreateConnection (System.Data.Common.DbConnectionOptions options, System.Data.Common.DbConnectionPoolKey poolKey, System.Object poolGroupProviderInfo, System.Data.ProviderBase.DbConnectionPool pool, System.Data.Common.DbConnection owningConnection, System.Data.Common.DbConnectionOptions userOptions) [0x00000] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection (System.Data.Common.DbConnection owningConnection, System.Data.ProviderBase.DbConnectionPoolGroup poolGroup, System.Data.Common.DbConnectionOptions userOptions) [0x00015] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection (System.Data.Common.DbConnection owningConnection, System.Threading.Tasks.TaskCompletionSource1[TResult] retry, System.Data.Common.DbConnectionOptions userOptions, System.Data.ProviderBase.DbConnectionInternal oldConnection, System.Data.ProviderBase.DbConnectionInternal& connection) [0x001d1] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal (System.Data.Common.DbConnection outerConnection, System.Data.ProviderBase.DbConnectionFactory connectionFactory, System.Threading.Tasks.TaskCompletionSource1[TResult] retry, System.Data.Common.DbConnectionOptions userOptions) [0x00036] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection (System.Data.Common.DbConnection outerConnection, System.Data.ProviderBase.DbConnectionFactory connectionFactory, System.Threading.Tasks.TaskCompletionSource1[TResult] retry, System.Data.Common.DbConnectionOptions userOptions) [0x00000] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.ProviderBase.DbConnectionInternal.OpenConnection (System.Data.Common.DbConnection outerConnection, System.Data.ProviderBase.DbConnectionFactory connectionFactory) [0x00000] in <290425a50ff84a639f8c060e2d4530f6>:0 at System.Data.Odbc.OdbcConnection.Open () [0x0000d] in <290425a50ff84a639f8c060e2d4530f6>:0 at DatabaseConnect.ReadACCDB (System.String fileToReadFrom) [0x00050] in D:\Abasyn Techno\Unity DAta BAckups\Factory\All In One\Assets\Scripts\Database\DatabaseConnect.cs:36 UnityEngine.Debug:Log(Object) DatabaseConnect:ReadACCDB(String) (at Assets/Scripts/Database/DatabaseConnect.cs:46) DatabaseConnect:Start() (at Assets/Scripts/Database/DatabaseConnect.cs:20)

Absinthe
  • 3,258
  • 6
  • 31
  • 70
aqeel
  • 87
  • 1
  • 13

0 Answers0