ODBC连接到.accdb文件 [英] ODBC connection to an .accdb file
问题描述
我正在尝试从我一直在从事的一个统一项目中访问Microsoft 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
它似乎无法找到该文件,但该文件位于该位置,有人知道为什么驱动程序在我的情况下不起作用吗?
找到了一种使用自定义系统DSN来解决该问题的方法
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();
}
}
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?
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();
}
}
这篇关于ODBC连接到.accdb文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!