Search Unity

Accessing SQL Server in Unity - An existing connection was forcibly closed by the remote host.

Discussion in 'Multiplayer' started by Deleted User, Jan 27, 2016.

  1. Deleted User

    Deleted User

    Guest

    This was originally posted in scripting, but no help was given, I thought maybe the people who look on the multiplayer networking section might be able to help more. Forgive me for double posting, I don't know how to delete the previous question.


    In my game, a player can(well, should be able to) create an account for storage in the database, so that they may upload custom levels for everyone to play. Unfortunately however, I cannot get the game to connect to the database. I have tested it with a standalone application, which calls the method that is in one of my scripts. So when the standalone application runs this method, everything works perfectly, and it creates and logs in the new user. When I call the exact same method from unity, it fails and gives the
    SocketException: An existing connection was forcibly closed by the remote host.
    exception.

    The database is on my local machine for now, and I have done quite a bit of googling attempting to fix this. I found one answer saying that I needed to allow TCP connections to my database and then restart it and it should work. I have done this, and nothing has changed.

    Here is my code for connecting:

    Code (CSharp):
    1.  private static string Server = "SAM-PC\\CHRONO3D";
    2.         private static string DB = "Main";
    3.         private static string preAuthConnString = @"Data Source=SAM-PC\CHRONO3D;Initial Catalog=Main;User ID=preauthenticatedUser;Password=nopassword";
    4.              //(new SqlConnectionStringBuilder()
    5.              //{
    6.              //    DataSource = Server,
    7.              //    InitialCatalog = DB,
    8.              //    UserID = "preauthenticatedUser",
    9.              //    Password = "nopassword"
    10.              //}).ToString();
    11.  
    12. //I have attempted creating the connection string both ways shown above, both give the same result.
    13.  
    14.         public static bool CreateUser(string userName, string password)
    15.         {
    16.        
    17.             using (SqlConnection conn = new SqlConnection(preAuthConnString))
    18.             {
    19.                  conn.Open(); //Exception occurs here
    20.    
    21.                 SqlCommand cmd = conn.CreateCommand();
    22.                 cmd.CommandType = System.Data.CommandType.StoredProcedure;
    23.                 cmd.CommandText = "CreateLogin";
    24.                 cmd.Parameters.AddWithValue("@User_Name", userName);
    25.                 cmd.Parameters.AddWithValue("@Password", password);
    26.                 int returnCode = (Int32)cmd.ExecuteScalar();
    27.  
    28.                 if (returnCode == 0)
    29.                 {
    30.                     return true;
    31.                     //Successful creation of user.
    32.                 }
    33.                 else
    34.                 {
    35.                     return false;
    36.                     //Creation failed. User probably already exists.
    37.                 }
    38.          
    39.             }
    40.         }


    This is the standalone program that works perfectly.
    Code (CSharp):
    1.  
    2. using System;
    3. using Assets.DAO;
    4.  
    5.  
    6. namespace TestDatabase
    7. {
    8.     class Program
    9.     {
    10.  
    11.         static void Main(string[] args)
    12.         {
    13.        
    14.                LevelEditorDAO.CreateUser("NEWLOGIN1342", "fdsfds");
    15.                LevelEditorDAO.Login("NEWLOGIN1342", "fdsfds");
    16.         }
    17.     }
    18. }
    19.  
    This is the method in unity that gets called that gets the exception.
    Code (CSharp):
    1. public void CreateAccount()
    2.     {
    3.         string un = GameObject.Find("DesiredUsername").GetComponent<InputField>().text;
    4.         string pw = GameObject.Find("DesiredPassword").GetComponent<InputField>().text;
    5.         if(LevelEditorDAO.CreateUser(un, pw))
    6.         {
    7.        
    8.             //LevelEditorDAO.Login(un, pw);
    9.             Create.SetActive(false);
    10.             CustomMenu.SetActive(true);
    11.             print("create account success");
    12.         }
    13.         else
    14.         {
    15.             print("create account failure");
    16.             //Username taken or something
    17.         }
    18.      
    19.     }
     
  2. junaidshad

    junaidshad

    Joined:
    Jul 10, 2017
    Posts:
    2
    did you find the fix yet? i'm actually getting the same error (when i open a connection i get this error --> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host)
     
  3. youssuf212

    youssuf212

    Joined:
    Mar 27, 2020
    Posts:
    2
    Did you find a solution yet?
     
  4. AKComp

    AKComp

    Joined:
    Aug 22, 2014
    Posts:
    26
    No solution ?