ODBC connection to an .accdb file

核能气质少年 提交于 2019-12-08 06:44:12

问题


I am trying to access a microsoft Access database file from a unity project i've been working on, but it keeps throwing an exception because it is unable to find the file and there has been no standard river has been selected.

The Code:

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Data;
using System.Data.Odbc;

public class AccDBReader : 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 = "FestoMES.accdb";
    Table_Name = "tblResource";
    Column_name = "ResourceName";

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

internal void ReadACCDB(string fileToReadFrom)
{
    //string connection = "Driver = {FestoODBCTest}; Dbq = " + fileToReadFrom +"; Uid = ; Pwd = ;";
    string connection = "Driver ={ Microsoft Access Driver(*.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";
    }
}

}

This is the console log after the program has attempted to run:

The connection string

Driver ={ Microsoft Access Driver(*.mdb, *.accdb)}; Dbq = C:/Users/ASJ/Desktop/ODBC connections and Access/Assets/FestoMES.accdb;

System.Data.Odbc.OdbcException: Error [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified. at System.Data.Odbc.OdbcConnection.Open()[0x00000] in <filename unkown>:0

Didnt find a table

It doesnt seem to be able to find the file, yet the file exists at that position, does anyone have an idea of why the driver doesnt work in my case?


回答1:


Figured out a way to solve it using a custom System DSN instead

internal void ReadACCDB()
{
    OdbcConnection conn = new OdbcConnection();
    conn.ConnectionString = "FIL=MS ACCESS;DSN=FestoACCDBTest";

    try
    {
        conn.Open();
        OdbcCommand dbCommand = conn.CreateCommand();
        dbCommand.CommandText = "SELECT ONo FROM tblFinOrder";
        OdbcDataReader dbReader = dbCommand.ExecuteReader();
        for (int i = 1; i < dbReader.FieldCount; i++)
        {
            testtext.text += " | " + dbReader.GetName(i);
        }
    }
    catch(Exception ex)
    {
        testtext.text = ex.ToString();
    }
    finally
    {
        conn.Close();
    }
}


来源:https://stackoverflow.com/questions/42906303/odbc-connection-to-an-accdb-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!