- Home /
SqlConnection SocketException only in Unity
I have an SQL server database running locally on SQL server management 18
When I run the following script to make a query in a non-unity console application, it works great
static void Main(string[] args)
{
SqlConnection connection = new SqlConnection(@"Server=localhost\SQLEXPRESS;Database=MY_DATABASE;User ID=sa;Password=;");
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM dataitems", connection);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader[2]);
}
}
}
However, when I run the same code in Unity, I get the error: "SocketException: An existing connection was forcibly closed by the remote host." (In both editor and built versions)
void Start()
{
SqlConnection connection = new SqlConnection(@"Server=localhost\SQLEXPRESS;Database=MY_DATABASE;User ID=sa;Password=;");
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM dataitems", connection);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Debug.Log(reader[2]);
}
}
}
Why is this happening on my machine? On other machines there doesn't seem to be a problem.
Common solutions I have already addressed:
My firewall is switched off
TCP/IP is enabled in the server network configuration
I would take a look at __ this __answer:
This generally means that the remote side closed the connection (usually by sending a TCP/IP RST packet). If you're working with a third-party application, the likely causes are:
You are sending malformed data to the application (which could include sending an HTTPS request to an HTTP server)
The network link between the client and server is going down for some reason
You have triggered a bug in the third-party application that caused it to crash
The third-party application has exhausted system resources
It's likely that the first case is what's happening.
Also this answer:
I got the same issue while using .NET Framework 4.5. However, when I update the .NET version to 4.7.2 connection issue was resolved. Maybe this is due to SecurityProtocol support issue.
Try upgrading the framework that you're using in Unity, from .Net 2.0 to .Net 4.x, which you can do in the build settings -> Other settings
And also try this answer if the above doesn't help
thanks for the reply, :) been googling for days so I already tried those not sure how it could be the first one if I am using basically the same code in each case
Answer by michaelwrightpennant · Apr 07, 2021 at 09:22 AM
I fixed it! And I'm going to be a good person and return to this post and share it with any lost travellers.
I needed to enable the server brower
to do this: go into Sql Server Configuration Manager, click Sql Server Services, right click Sql Server Browser, Properties, Service, Start Mode Automatic, Apply, right click again, Start.
Your answer
Follow this Question
Related Questions
How to connect to a MSSQL server through Unity editor/standalone? 2 Answers
VR Highscore with database (SQLite) 1 Answer
Image and SQL Database 1 Answer
save load exp on database 2 Answers
Sql Server Compact 4.0 error 2 Answers