如何从Unity连接到数据库
我试图通过Unity连接到MS SQL数据库。但是,当我尝试打开连接时,我收到IOException:连接丢失。
我从Unity \ Editor \ Data \ Mono \ lib \ mono \ 2.0导入了System.Data.dll。我使用以下代码:
using UnityEngine; using System.Collections; using System.Data.Sql; using System.Data.SqlClient; public class SQL_Controller : MonoBehaviour { string conString = "Server=myaddress.com,port;" + "Database=databasename;" + "User ID=username;" + "Password=password;"; public string GetStringFromSQL() { LoadConfig(); string result = ""; SqlConnection connection = new SqlConnection(conString); connection.Open(); Debug.Log(connection.State); SqlCommand Command = connection.CreateCommand(); Command.CommandText = "select * from Artykuly2"; SqlDataReader ThisReader = Command.ExecuteReader(); while (ThisReader.Read()) { result = ThisReader.GetString(0); } ThisReader.Close(); connection.Close(); return result; } }
这是我得到的错误:
IOException: Connection lostMono.Data.Tds.Protocol.TdsComm.GetPhysicalPacketHeader ()Mono.Data.Tds.Protocol.TdsComm.GetPhysicalPacket ()Mono.Data.Tds.Protocol.TdsComm.GetByte ()Mono.Data.Tds.Protocol.Tds.ProcessSubPacket ()Mono.Data.Tds.Protocol.Tds.NextResult ()Mono.Data.Tds.Protocol.Tds.SkipToEnd ()Rethrow as TdsInternalException: Server closed the connection.Mono.Data.Tds.Protocol.Tds.SkipToEnd ()Mono.Data.Tds.Protocol.Tds70.Connect (Mono.Data.Tds.Protocol.TdsConnectionParameters connectionParameters)Mono.Data.Tds.Protocol.Tds80.Connect (Mono.Data.Tds.Protocol.TdsConnectionParameters connectionParameters)
请用这种方法忽略任何安全风险,我需要这样做进行测试,安全性会在以后发生。感谢您的时间。
蝴蝶刀刀
吃鸡游戏
相关分类