How can I connect the database and catch a value by Access

It always show errors

“System.Data.Odbc.OdbcException: ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver] Could not find file ‘(unknown)’.
at System.Data.Odbc.OdbcConnection.Open () [0x00000] in :0
UnityEngine.Debug:Log(Object)
RaderData:ReadStudent(String) (at Assets/Connection/Scripts/RaderData.cs:55)
RaderData:Start() (at Assets/Connection/Scripts/RaderData.cs:16)”

This is my code:

using UnityEngine;
using System.Collections;
using System;
using System.Data;
using System.Data.Odbc;
public class RaderData : MonoBehaviour{
    string text = string.Empty;

    public void Start(){

        ReadStudent(Application.dataPath + "/Nutrap.accdb");

    }

    internal void ReadStudent(string filetoread){

        string connection = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" + filetoread;          
        string sqlQuery = "select * from Student";
        OdbcConnection con = new OdbcConnection(connection);
        OdbcCommand cmd = new OdbcCommand(sqlQuery,con);
        DataTable dt = new DataTable("Student");

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

        catch (Exception ex){
			//text = dt.Rows[3][1].ToString();
            Debug.Log(ex.ToString());
        }

        finally{
            if (con.State!=ConnectionState.Closed){
                con.Close();
            }
            con.Dispose();
        }

        if (dt.Rows.Count>0){
                text = dt.Rows[1][1].ToString();
        }
        
    }

    public void OnGUI(){
        GUI.Label(new Rect(10,10,500,200),text);
    }        
    
}

Thanks for the help :frowning:

1 Answer

1

I’m pretty sure that’s just it not being able to find your “/Nutrap.accdb” file.

Do a

Debug.Log(connection)

after that variable is set, and check that the file-path matches the reality on your pc.