With this kind of set-up you would generally call the the unity app the "client", because it's the part of the system which the end-user uses to view and interact with the world.
您的客户通常不会直接访问数据库。这通常代表一个重大的安全问题。更典型的是,客户端会使用套接字连接与专用的多用户服务器应用程序通信(例如,SmartFox 服务器之类的东西,或者通常 - 对于 MMO 来说 - 自定义编写的服务器应用程序)。
由于类似的安全原因,更简单的数据库交互(例如服务器端记分板)也是如此,您的客户端永远不会直接访问数据库。相反,您的客户端(游戏)会与专门编写的服务器端脚本(例如,PHP 或 ASP)对话,而这些脚本本身会访问数据库。
因此,只有在您完全控制下的服务器端程序才有权直接访问数据库,而您的客户端(不太受信任,因为它们在您的用户手中!)仅限于提高通过您自己设计的 API 请求级别请求,在分数的情况下仅限于相关命令,例如“SubmitScore”和“RetrieveScoreboard”,或“MoveToLocation”、“TakeItem”、“Say Message”等多人角色扮演游戏。
然后,这个多用户服务器将处理您世界中的交互,并负责与幕后的数据库进行交互,以创建、读取、更新和删除数据库中的信息,例如用户详细信息和持久性世界数据。
因此,您的 Unity 客户端永远不需要知道诸如 SQL、表、记录等之类的东西的存在 :-) 这些东西都应该保留在您的服务器上,无论是作为服务器脚本还是在您的多用户服务器中申请。
public void connect(){
string connectionString =
"Server=servername;" +
"Database=dbname;" +
"User ID=userid;" +
"Password=pass;" +
"Integrated Security=True";
result = new List<float> ();
resultCas = new List<DateTime> ();
using(SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand c; SqlDataReader da; SqlParameter param1; SqlParameter param2; SqlParameter param3; SqlParameter param4;
conn.Open();
c = new SqlCommand();
c.Connection = conn;
c.CommandType = CommandType.StoredProcedure;
c.CommandText = "commandtext";
param1 = c.Parameters.Add("@identify",SqlDbType.Int);
param1.Value = 1;
param2 = c.Parameters.Add("@startTime",SqlDbType.DateTime);
param2.Value = "2010-11-10 07:45:00.000";
param3 = c.Parameters.Add("@endTime",SqlDbType.DateTime);
param3.Value = "2010-11-12 10:15:00.000";
param4 = c.Parameters.Add("@args",SqlDbType.NVarChar);
param4.Value = "I";
da = c.ExecuteReader();
while (da.Read())
{
resultCas.Add(da.GetDateTime(0));
result.Add((float)da.GetDouble(1));
}
}
}