When I connect to Access2007(*.accdb), an error occurs:
DllNotFoundException: gda-2 System.Data.OleDb.OleDbConnection.Open () (wrapper remoting-invoke-with-check) System.Data.OleDb.OleDbConnection:Open () AccessReader.ConnectAccdb (System.String path) (at Assets/AccessReader.cs:36)
AccessReader.Start () (at Assets/AccessReader.cs:13)
The file system.data.dll is copied from “unity/editor” to “my project/assets”. I thought may be the version of system.data.dll is not correct , so I search all the versions of system.data.dll from my harddisk-C and copy them to “my project/assets”, but each of them will cause an error.
The script of connect db as follows:
using UnityEngine;
using System.Collections;
using System.Data;
using System.Data.OleDb;
public class AccessReader : MonoBehaviour
{
// Use this for initialization
void Start ()
{
string path = @"E:\u3d_proj\access mdb\Assets\test.accdb";
DataTable dt = ConnectAccdb(path);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
print(dr["name"].ToString());
print(dr["age"].ToString());
print(dr["alive"].ToString());
}
}
// Update is called once per frame
void Update()
{
}
DataTable ConnectAccdb (string path)
{
DataTable dt = new DataTable ();
DataRow dr;
try {
OleDbConnection con = new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path);
con.Open ();
OleDbCommand cmd = con.CreateCommand ();
cmd.CommandText = "select * from musician where name = 'chopin'";
OleDbDataReader rdr = cmd.ExecuteReader ();
int size= rdr.FieldCount;
for (int i=0;i <size;i ++)
{
DataColumn dc=new DataColumn (rdr.GetName( i));
dt.Columns.Add(dc);
}
while (rdr.Read())
{
dr = dt.NewRow();
for (int i = 0; i < size; i++)
{
dr[rdr.GetName(i)] = rdr[rdr.GetName(i)].ToString();
}
dt.Rows.Add(dr);
}
rdr.Close();
con.Close();
return dt;
} catch (OleDbException e) {
Debug.LogError (e.ToString ());
return null;
}
}
}