雖然在Unity3D中能夠通過PlayerPrefs類來保存和讀取數(shù)據(jù),但是一旦數(shù)據(jù)量增大,僅僅通過代碼的方式存取數(shù)據(jù),這樣的工作量是非常大的。那么如何通過使用Sql Server數(shù)據(jù)庫來存取數(shù)據(jù)呢?其實(shí)過程也非常簡(jiǎn)單,過程如下:
1、找到System.Data.dll文件,默認(rèn)的地址是在C:/Program Files/Unity/Editor/Data/Mono/lib/mono/unity,這個(gè)根據(jù)你所安裝的路徑有關(guān)。
	
2、將該文件復(fù)制到你的工作空間下的Asset文件夾內(nèi)
3、在你的編輯器中添加引用,我用的是VS
	
	
4、在命名空間內(nèi)增加程序集
using System;
using System.Data;
using System.Data.SqlClient;
5、編寫連接數(shù)據(jù)庫代碼
SqlConnection con = null;    SqlDataAdapter sda = null;    void Start()    {        string s = @"server=.;database=ConnectTest;uid=sa;pwd=123456";    //注意,這里必須使用SQL Server和Windows驗(yàn)證模式,否則會(huì)報(bào)錯(cuò)        con = new SqlConnection(s);        con.Open();        string sql = "select * from table1";        sda = new SqlDataAdapter(sql, con);        DataSet ds = new DataSet();        sda.Fill(ds, "table1");        print(ds.Tables[0].Rows[0][0]);    }6、實(shí)驗(yàn)結(jié)果
	
新聞熱點(diǎn)
疑難解答
圖片精選