转到Unity3D的安装目录下,将Unity\Editor\Data\Mono\lib\mono\unity下的I18N.dll,I18N.West.dll,I18N.CJK.dll及System.Data.dll复制到unity工程的Assets下,在脚本中添加using System.Data.SqlClient;然后浏览添加System.Data引用;则可以在脚本中使用SqlConnection连接SQLServer了。
下面为远程登录测试
try
{
string strConn = @"server=169.254.224.39;database=DropScore;uid=sa;pwd=1234";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string strCmd = "insert into table_drop_score values('" + str_PlayerName + "','" + Score + "') ";
//select top 10 *from table_drop_score order by drop_score desc 获取前十名
print(strCmd);
SqlCommand cmd = new SqlCommand(strCmd, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
catch (SqlException ex)
{
print(ex.Message);
print("服务器不存在或者错误,暂时不提供分数上传功能!");
return false;
}