Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KotiaCoder · Aug 26, 2016 at 04:45 AM · networkingsqlsqldatabase

How to connect to a MSSQL server through Unity editor/standalone?

Hello.

I am trying to connect to a MS SQL database through Unity. However, when I try to open a connection, I get an IOException: Connection lost.

I have imported System.Data.dll from Unity\Editor\Data\Mono\lib\mono\2.0. I am using the following code:

 using UnityEngine;
 using System.Collections;
 using System.Data.Sql;
 using System.Data.SqlClient;
 
 public class SQL_Controller : MonoBehaviour {
 
     string conString = "Server=myaddress.com,port;" +
             "Database=databasename;" +
             "User ID=username;" +
             "Password=password;";
 
     public string GetStringFromSQL()
     {
         LoadConfig();
         string result = "";
         
         SqlConnection connection = new SqlConnection(conString);
         connection.Open();
         Debug.Log(connection.State);
         SqlCommand Command = connection.CreateCommand();
         Command.CommandText = "select * from Artykuly2";
         SqlDataReader ThisReader = Command.ExecuteReader();
         while (ThisReader.Read())
         {
             result = ThisReader.GetString(0);
         }
         ThisReader.Close();
         connection.Close();
 
         return result;
     }
 }



This is the error I get:

 IOException: Connection lost
 Mono.Data.Tds.Protocol.TdsComm.GetPhysicalPacketHeader ()
 Mono.Data.Tds.Protocol.TdsComm.GetPhysicalPacket ()
 Mono.Data.Tds.Protocol.TdsComm.GetByte ()
 Mono.Data.Tds.Protocol.Tds.ProcessSubPacket ()
 Mono.Data.Tds.Protocol.Tds.NextResult ()
 Mono.Data.Tds.Protocol.Tds.SkipToEnd ()
 Rethrow as TdsInternalException: Server closed the connection.
 Mono.Data.Tds.Protocol.Tds.SkipToEnd ()
 Mono.Data.Tds.Protocol.Tds70.Connect (Mono.Data.Tds.Protocol.TdsConnectionParameters connectionParameters)
 Mono.Data.Tds.Protocol.Tds80.Connect (Mono.Data.Tds.Protocol.TdsConnectionParameters connectionParameters)
 System.Data.SqlClient.SqlConnection.Open ()
 

Please disregard any security risks with this approach, I NEED to do this for testing, security will come later. Thank you for your time.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jeffblumenthal · Jan 10, 2017 at 07:46 AM

Pull in the Mono.Data, Mono.Data.Tds, and Mono.Data.TdsClient dlls too.

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
avatar image
0

Answer by mholmes · Apr 02, 2018 at 09:11 PM

Here is some recommended syntax:

sample:

 DataSet ds = new DataSet();
                 int rows_returned;
                 string sqlQuery =
                             "SELECT TOP 1 " +
                                 "[ID]," +
                                 "[Name]" +
                             "FROM " +
                                 "[dbo].[YourTableNameHere] " +
                             "WHERE " +
                                 "[ID] = '" + ID + "'";
 
                 using (SqlConnection connection = new SqlConnection(cn))
                 using (SqlCommand cmd = connection.CreateCommand())
                 using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                 {
                     cmd.CommandText = sqlQuery;
                     cmd.CommandType = CommandType.Text;
                     connection.Open();
                     rows_returned = sda.Fill(ds);
                     connection.Close();
                 }
 
                 if(rows_returned > 0)
                 {
                     return ds;
                 }
                 else
                 {
                     return null;
                 }


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Save and Load JSON Data to and from Webserver, Performance Question 0 Answers

I already made a ar app which deploys flag, i want to upgrade it to make the flag stay there at the exact geo location 1 Answer

save load exp on database 2 Answers

Unity/C# - Is it possible to connect an Azure (cloud) database, to a Unity game, via EnityFramework? 1 Answer

SqlConnection SocketException only in Unity 1 Answer


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