Transferred from: http://unity.jb51.net/chengxukaifa/shujukubiancheng/1104.html
Directly shared with you using unity to connect to SQL Server. And a lot of friends. Use Unity to connect to access
Share the next Unity connect access here for everyone
You need to use two external references first. System.Data.dll and System.EnterpriseServices.dll
Next, directly post the code:
using unityengine;using system.collections;//Introducing Namespaces Using system;using system.data;using System.Data.Odbc;public class RaderData : MonoBehaviour { /// <summary> /// declares a variable that accepts the value of the Read data field /// </summary> string text = string. Empty; public void start () { //read the data file. readstudent (application.datapath + "/Wild BOAR.ACCDB "); } /// <summary> function /// </summary> /// <param for reading table values name= the path to the "Filetoread" > Data file </param> internal void readstudent ( String filetoread) { //declaring fields that connect to a database string connection = "driver={microsoft access driver (*. MDB,&NBSP;*.ACCDB)}; dbq= " + filetoread; //query all data from the table string sqlquery = "Select * from student"; //Open Database odbcconnection con = new odbcconnection ( Connection) //operation of the database odbccommand cmd = new odbccommand (Sqlquery,con); //reads data to a temporary table based on the table name datatable dt = new datatable ("Student") //try catch finally was treated with exception, this is a good habit, hehe try { //Open Database con. Open (); //Read Data odbcdatareader reader = cmd. ExecuteReader (); //loading data into temporary tables dt. Load (reader); //after use, be sure to close it, or else it will be a problem reader. Close (); //Closes the database &nBsp; con. Close (); } catch (EXCEPTION&NBSP;EX) { debug.log (ex. ToString ()); } finally { //determines whether the database is open or closed if it is opened. if (Con. state!=connectionstate.closed) { con. Close (); } //frees the database resource con. Dispose (); } if (dt. rows.count>0) { //reading Data for (Int i = 0; i < dt. rows.count; i++) { text = dt. Columns[0]. columnname + " : " + dt. Rows[dt. Columns[0]. ColumnName]. ToString () + " | " + dt. COLUMNS[1]. columnname + " : " + dt. Rows[dt. COLUMNS[1]. ColumnName]. ToString () + " | " + dt. COLUMNS[2]. columnname + " : " + dt. Rows[dt. COLUMNS[2]. ColumnName]. ToString () + " | " + dt. COLUMNS[3]. columnname + " : " + dt. Rows[dt. COLUMNS[3]. ColumnName]. ToString (); Debug.Log (dt. Columns[0]. columnname + " : " + dt. Rows[dt. Columns[0]. ColumnName]. ToString () + " | " + dt. COLUMNS[1]. columnname + " : " + dt. Rows[dt. COLUMNS[1]. ColumnName]. ToString () + " | " + dt. COLUMNS[2]. columnname + " : " + dt. Rows[dt. COLUMNS[2]. ColumnName]. ToString () + " | " + dt. Columns[3]. columnname + " : " + dt. Rows[dt. COLUMNS[3]. ColumnName]. ToString ()); } } } public void ongui () { gui. Label (New rect (10,10,500,200), text); }}
dll files : http://unity.jb51.net/yuanma/1105.html
What is the problem can leave a message oh ...
can also enter http://unity.jb51.net/ask/ here. Come and ask your question ...
Connect and read an Access database using unity