unity中对access数据库的使用
1、在access数据库中创建的对应的数据库表,并将数据库表托放在unity的assets目录文件下。

2、创建链接数据库脚本。 需引入以下命名空间using System;using System.Data;using System.Data.Odbc;
3、直接上代码:public class ConnetionAccess : MonoBehaviour { public int pointAllNum=0; // Use this for initialization void Update () { } string accessUrll = ""; void Awake(){ accessUrll = Application.dataPath + "/untiyAccess.mdb"; } /// <summary> /// 声明一个接受读取数据字段值的变量 /// </summary> string text = string.Empty; public void Start() { } /// <summary> /// 获取表的数据 /// findSql sql语句, filetoread .mdb文件地址 /// </summary> /// <param name="findSql">Find sql.</param> /// <param name="filetoread">Filetoread.</param> public DataTable GetAccessTable(string findSql,string filetoread){ //filetoread = Application.dataPath+"/untiyAccess.mdb"; // print ("路径:"+filetoread); return ReadStudent (findSql,filetoread); } string tableName = "v_fma_class_relation";//RelationShipTable /// <summary> /// 读取表数值的函数 /// </summary> /// /// <param name="filetoread">数据文件的路径</param> internal DataTable ReadStudent(string findSql,string filetoread) { //声明连接数据库的字段 string connection = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" + filetoread; //从表中查询所有数据 string sqlQuery = findSql; //打开数据库 OdbcConnection con = new OdbcConnection(connection); //对数据库进行操作 OdbcCommand cmd = new OdbcCommand(sqlQuery,con); //根据表名,读取数据到一个临时表 DataTable dt = new DataTable(tableName); //try catch finally进行了异常 处理,这个是好习惯,嘿嘿 try { //打开数据库 con.Open(); //读取数据 OdbcDataReader reader = cmd.ExecuteReader(); //把数据加载到临时表 dt.Load(reader); //在使用完毕之后,一定要关闭,要不然会出问题 reader.Close(); //关闭数据库 con.Close(); } catch (Exception ex) { Debug.Log(ex.ToString()); } finally { //判断数据库是否打开,如果打开就关闭。 if (con.State!=ConnectionState.Closed) { con.Close(); } //释放数据库资源 con.Dispose(); } //print (dt.Rows.Count); if (dt.Rows.Count > 0) { //读取数据 for (int i = 0; i < dt.Rows.Count; i++) { string name = dt.Rows[i]["name"].ToString(); print (name); } } return dt; } //查询数据库所有数据 public DataTable selectPoint(){ return GetAccessTable ("select * from test ",accessUrll); }//添加数据到数据库中 public void addPoint(int id,string name,Vector3 pos,Vector3 rot,string plcNum, string boundName,string remark,string types){//本人自己的数据库表 需按自己的需要来拼接sql语句 GetAccessTable ("insert into test values(" + id+",'"+name+"',"+pos.x+","+pos.y+","+pos.z+","+rot.x+","+rot.y+","+rot.z+",'"+plcNum+"','"+boundName+"','"+remark+ "',0,'"+types+"')",accessUrll); } // 可以执行 真删除 public void deletePoint( int id){ GetAccessTable ("delete from test where id = " + id ,accessUrll); } //逻辑删除 del_flag = 1; public void deletePoint1(int id){ GetAccessTable ("update test set del_flag = 1 where id = "+id,accessUrll); } //修该数据 public void changePoint( string cloName,string value,int id){//列名,对应值,对应id print ("update test set "+cloName+" = '"+value+"' where id ="+id); GetAccessTable ("update test set "+cloName+" = '"+value+"' where id ="+id,accessUrll); } public DataTable findPoint(string SXname,string SXvalue){//属性名称 属性值 根据其中一个属性查找对应监测点的方法、、查询需要返回一个集合 return GetAccessTable ("select * from test where "+SXname+"='"+SXvalue+"'",accessUrll); }}