unity3d怎么連接sql server數(shù)據(jù)庫?
雖然在Unity3D中能夠通過PlayerPrefs類來保存和讀取數(shù)據(jù),但是一旦數(shù)據(jù)量增大,僅僅通過代碼的方式存取數(shù)據(jù),這樣的工作量是非常大的。那么如何通過使用Sql Server數(shù)據(jù)庫來存取數(shù)據(jù)呢?其實過程也非常簡單,過程如下:
1、找到System.Data.dll文件,默認的地址是在C:\Program Files\Unity\Editor\Data\Mono\lib\mono\unity,這個根據(jù)你所安裝的路徑有關(guān)。

2、將該文件復制到你的工作空間下的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驗證模式,否則會報錯
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、實驗結(jié)果

關(guān)鍵詞:unity3d,sql,數(shù)據(jù)庫
閱讀本文后您有什么感想? 已有 人給出評價!
- 1
- 1
- 1
- 1
- 1
- 1
