Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by michaelwrightpennant · Apr 03, 2021 at 01:25 PM · sqlsqldatabase

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Apr 03, 2021 at 05:57 PM 0
Share

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

avatar image michaelwrightpennant Llama_w_2Ls · Apr 06, 2021 at 08:07 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges