ODBC连接到.accdb文件

时间:2017-03-20 14:32:59

标签: unity3d odbc unity5

我正在尝试从我一直在处理的统一项目中访问微软Access数据库文件,但它不断抛出异常,因为它无法找到该文件,并且没有选择标准河流。 / p>

守则:

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

}

这是程序尝试运行后的控制台日志:

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

它似乎无法找到该文件,但该文件存在于该位置,是否有人知道为什么驱动程序在我的情况下不起作用?

1 个答案:

答案 0 :(得分:0)

找出一种使用自定义系统DSN来解决它的方法

tex.allocateStorage