Search Unity

Bug Unity MS SQL - An existing connection was forcibly closed by the remote host.

Discussion in 'Scripting' started by Haewon_2003, Dec 9, 2022.

  1. Haewon_2003

    Haewon_2003

    Joined:
    Jun 2, 2022
    Posts:
    7
    Hello everyone, I have encountered a problem connecting to MS SQL at my local computer.

    An existing connection was forcibly closed by the remote host.

    Connectstring:
    Server=haewon2003\SQLEXPRESS;Database=sunkingdom;Trusted_Connection=True;

    Unity:
    .NET Framework


    My Winform app can still connect with MS SQL.
    I try install MS SQL in my ubuntu VPS and connect Unity game (unity in my local pc) with ip sql of vps, it's work but in local error

    My code
    Code (CSharp):
    1. public static SqlConnection cnn;
    2.     void Start()
    3.     {
    4.         cnn = new SqlConnection(@"Server=haewon2003\SQLEXPRESS;Database=sunkingdom;Trusted_Connection=True;");
    5.         cnn.Open();
    6.  
    7.  
    8.         SqlCommand command;
    9.         SqlDataReader dataReader;
    10.         string query = $"SELECT * FROM TestConnect";
    11.  
    12.         try
    13.         {
    14.             command = new SqlCommand(query, cnn);
    15.  
    16.             dataReader = command.ExecuteReader();
    17.             while (dataReader.Read())
    18.             {
    19.                 GetValue(dataReader.GetValue(0));
    20.             }
    21.  
    22.             dataReader.Close();
    23.             command.Dispose();
    24.  
    25.  
    26.         }
    27.         catch (Exception ex)
    28.         {
    29.            
    30.         }
    31.  
    32.     }
    My error


    SocketException: An existing connection was forcibly closed by the remote host.
    System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () (at <a6f433c7914f4a8aa11562de443cf6c1>:0)
    System.Net.Sockets.Socket.EndReceiveFrom (System.IAsyncResult asyncResult,
    System.Net.EndPoint& endPoint) (at <a6f433c7914f4a8aa11562de443cf6c1>:0)
    System.Net.Sockets.UdpClient.EndReceive
    (System.IAsyncResult asyncResult,
    System.Net.IPEndPoint& remoteEP) (at <a6f433c7914f4a8aa11562de443cf6c1>:0)
    System.Net.Sockets.UdpClient.<ReceiveAsync>b__65_1 (System.IAsyncResult ar) (at <a6f433c7914f4a8aa11562de443cf6c1>:0)
    System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic
    (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction,
    System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise,
    System.Boolean requiresSynchronization)
    (at <c2a97e0383e8404c9fc0ae19d58f57f1>:0)
    Rethrow as AggregateException: One or more errors occurred.
    (An existing connection was forcibly closed by the remote host.


    Thanks.
     
  2. Haewon_2003

    Haewon_2003

    Joined:
    Jun 2, 2022
    Posts:
    7
    Please help me
     
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Trusted_Connection=True
    this only works if the SQL Server is in the same (Windows) PC as the app, but you're saying the server is now on a Linux VM, so you can't use Integrated Security (a.k.a Trusted Connection); So you should connect to your SQL Server with a username/password instead. Also check the server you specify in your connection string, since
    haewon2003\SQLEXPRESS
    is a Windows Domain thing, not a Linux thing. So your connection should probably be

    Code (CSharp):
    1. cnn = new SqlConnection(@"Server=IpAddress\SQLEXPRESS;Database=sunkingdom;User Id=sqlUsername;Password=sqlPassword;");
    Note: IpAddress can be just 127.0.0.1 or localhost if both SQL Server and your Unity app runs on the same Linux VM.

    Anyway, in all cases, I highly discourage you to connect to a database directly from a client, as anybody can access the server ip/username/password of the database by simply decompiling your client app. The best way is to use a Web app (API) which acts as an intermediary between your app and the database, which will also make a lot of problems like the one you have now simply disappear.
     
    Last edited: Dec 11, 2022
    Kurt-Dekker likes this.
  4. Haewon_2003

    Haewon_2003

    Joined:
    Jun 2, 2022
    Posts:
    7
    yep, it's work on vps but local window is error.