0
votes

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)

1
Connecting to a non-embedded database is generally not something you want to do directly in the kinds of applications you make with Unity. Typically you create a service as a "data layer" and make http calls to it instead of direct connection to the database. - Crowcoder
I believe Integrated Security only works on Windows. Try creating a user on SQL Server and add it to your connection string. Also, are you sure SQL Server is running on the machine? Don't you have to replace localhost? - tvdias
@tvdias first , thanks for the comment. i just created a user and changed the connection string. again works well in visual studio but same error in unity. and i edited the code, could you please check again - Siavash
While we are not sure of the bigger picture here, I highly recommend you do not embed your SQL connection data directly into the app and have all your clients connect directly to the DB. - Samuel Goldenbaum

1 Answers

0
votes

You cannot use ODBC in Unity due to limitations in Mono. Use http://github.com/dotnet/SqlClient instead.

Your specific error is related to the differences in how machine names are resolved in Mono versus the ancient version of .Net framework of the WinForms era. DESKTOP-PVB5MHG will not be a valid DNS name, while Mono only uses DNS to resolve Data Source.