0

Friends need your help! I'm doing game project as my qualification work in University. I do this in Unity3D. As main part of my project I must use databases in my qualification work. I have created database in SQL Server 2014 Express and trying to use this database in my Unity project. My code is below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data.SqlClient;
public class GObj : MonoBehaviour {

 public void WorkWithDatabase()
 {
     string connectionstring = @"Data Source=127.0.0.1;Initial Catalog=GameForDiplom;Integrated Security=True;";

     SqlConnection Myconnection = new SqlConnection(connectionstring);
     Myconnection.Open();
     Debug.Log("OPEN SUCCESS!");

     using (var cmd = Myconnection.CreateCommand())
     {
         cmd.CommandText = @"SELECT * FROM [User]";
         using (var rdr = cmd.ExecuteReader())
         {
             while (rdr.Read())
             {
                 Debug.Log(rdr["IDuser"] + "\t" + rdr["Nickname"] + "\n");
             }
         }
     }

     Myconnection.Close();
     Debug.Log("CLOSE SUCCESS!");
 }
}

During connection to my database in game i receive this

ERROR: SocketException: The connection is not established, since target machine actively refused the connection request.

Please, I would like to see working code in C# and your settings of SQL 2014 Express which you use. I tried to open TSP port 1433 but I receive this

ERROR: NullReferenceException: Object reference not set to an instance of an object Mono.Data.Tds.Protocol.TdsConnectionPool.GetConnection ()

Friends, i'm not pro programmers as you can see, but i'm trying to be better! Please, help!

Thank you for help, Friends!

Arcosha
  • 1
  • 5
  • If you must use database, don't use it from Unity directly. Use RESTful(WWW or UnityWebRequest) to connect to a server and then use php, perl or whatever server language you want to access the database. See [this](https://stackoverflow.com/questions/39140068/how-to-connect-to-a-mssql-server-database-through-unity3d-editor-standalone/39140295#39140295) for more information. – Programmer Dec 23 '17 at 10:55
  • Hello! Thank you for help. I have read that post. I did that way but professor said that I must use direct connection without any additional features of Unity3d :( – Arcosha Dec 23 '17 at 10:58
  • Have you tried disabling firewall? – Programmer Dec 23 '17 at 11:18
  • Yes, i turned off Firewall, but 1st ERROR message still there is. I opened TCP port 1433 in SQL Server 2014 Configuration manager BUT nothing happen =( – Arcosha Dec 23 '17 at 12:34
  • See [this](https://forum.unity.com/threads/sql-connection-error.46854/) post. I hope that should solve your issue. Note that your are not currently passing your username, password and port in the code. You may want to do that – Programmer Dec 23 '17 at 12:36
  • Programmer i changed Data Source to "MEIJIN-ПК\SQLEXPRESS" and now receive another message: **SocketException**: Remote host forcibly broke the existing connection. – Arcosha Dec 23 '17 at 12:46
  • Just Google that new error. See [this](https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host). I would start by changing the .NET version to 4.6 in Unity – Programmer Dec 23 '17 at 12:49
  • Programmer, I tried Google this question but all that decisions don't work =( – Arcosha Dec 23 '17 at 13:03
  • Programmer, if you have free time, pls try to install Unity and MSSQLSERVER EXPRESS 2014. Try to use my code except connection string ;) and try to connect your Unity3D project to your database. If it's possible send me you code and settings of SQL Server, firewall and all that need for correct connection. Thanks in advance! – Arcosha Dec 23 '17 at 13:17
  • Why are you using the loopback IP address 127.0.0.1? That is causing the error : "target machine actively refused the connection request." – jdweng Dec 23 '17 at 13:39
  • jdweng, Yes now i'm receiving this error too, as i understand it's default address of my computer. I have found this in my hosts file on PC. Actually i didn't study Server management therefore it's my selfeducation part =( – Arcosha Dec 23 '17 at 13:44
  • I tried to change Instance name of server; query was accomplished successfully but in properties of my server instance name hasn't been changed Q_Q Friends, sorry please that i'm so stupid in servers =( It's addtitional part of education and i don't have anyone to ask this question in my group :( – Arcosha Dec 23 '17 at 13:49
  • I made database in SQLite. MS SQL Server doesn't want to work with Unity without troubles. – Arcosha Feb 18 '18 at 15:30

0 Answers0