How to connect to a MSSQL server database through Unity3d editor/standalone?

I am trying to connect to a MS SQL database through Unity. However, when I try to open a connection, I get an IOException: Connection lost.

I have imported System.Data.dll from Unity\Editor\Data\Mono\lib\mono\2.0. I am using the following code:

  • Can't start sql server in single user mode
  • Sql server update multiple columns from another table
  • SQL Server: add values of small tables to the values of big table without losing the dimensions of the big table?
  • NHibernate transaction and race condition
  • Pagination on fast changing database content
  • How to replicate mysql range and limit in SQL Server using the top clause
  •  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;
         }
     }
    

    This is the error I get:

    IOException: Connection lost
    Mono.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)
    

    Please disregard any security risks with this approach, I NEED to do this for testing, security will come later.
    Thank you for your time.

  • What is the most cost-effective way to break up a centralised database?
  • Saving an Image file to sql Server and converting byte array into image
  • Can't return last inserted row from stored procedure
  • How to embed/attach SQL Database into Visual C#?
  • return a list of data via stored proc to dapper
  • “The model backing the 'DataContext' context has changed” but I am on the same migration
  • One Solution collect form web for “How to connect to a MSSQL server database through Unity3d editor/standalone?”

    Do not do this. It doesn’t matter if security will come before or after. You will end of re-writing the whole code. Do it the correct way now.

    Run your database command on your server with php, perl or whatever language you are comfortable with.

    From Unity, use the WWW or UnityWebRequest class to communicate with that script and be able able to send and receive information from your server. There are many examples out there. Even with this, you still need to implement your own security but this is much more better than what you have now.

    You can also receive data multiple with json.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.