I'm trying to connect my Unity project to SQL Server. I have connected my Windows Forms application in Visual Studio 2017 successfully. And I think if copy the exact code in a unity script my goal should happen. I have imported System.Data.Dll V3.5 to my assets and using unity2018.2.1.f1. I am using the following script trying to first open the connection but I get the error below.
void Start ()
{
try
{
string ConnString = "Data Source=DESKTOP-PVB5MHG;Initial Catalog=UnityTest;User ID=legend;Password=legend";
SqlConnection MyConn = new SqlConnection(ConnString);
MyConn.Open();
Debug.Log("Connection opened !");
}
catch(System.Exception ex)
{
Debug.Log(ex.ToString());
}
}
System.Data.SqlClient.SqlException: Server does not exist or connection refused.
Mono.Data.Tds.Protocol.TdsInternalException: Server does not exist or connection refused.
System.Net.Sockets.SocketException: An address incompatible with the requested protocol was used.
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in :0
at System.Net.Sockets.Socket+Worker.Connect () [0x00000] in :0
--- End of inner exception stack trace ---
at Mono.Data.Tds.Protocol.TdsComm..ctor (System.String dataSource, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion tdsVersion) [0x00000] in :0
at Mono.Data.Tds.Protocol.Tds..ctor (System.String dataSource, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion tdsVersion) [0x00000] in :0
at Mono.Data.Tds.Protocol.Tds70..ctor (System.String server, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion version) [0x00000] in :0 at Mono.Data.Tds.Protocol.Tds80..ctor (System.String server, Int32 port, Int32 packetSize, Int32 timeout) [0x00000] in :0 at Mono.Data.Tds.Protocol.TdsConnectionPoolManager.CreateConnection (Mono.Data.Tds.Protocol.TdsConnectionInfo info) [0x00000] in :0 at Mono.Data.Tds.Protocol.TdsConnectionPool.GetConnection () [0x00000] in :0 at System.Data.SqlClient.SqlConnection.Open () [0x00000] in :0 --- End of inner exception stack trace --- at System.Data.SqlClient.SqlConnection.Open () [0x00000] in :0 at DB.Start () [0x00010] in D:\UnityProjects2018\DataBaseTest1\Assets\DB.cs:15 UnityEngine.Debug:Log(Object) DB:Start() (at Assets/DB.cs:20)