• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by shockelite · Mar 31, 2015 at 05:18 AM · c#dllconnectionsqlquery

MSSQL Connection and Select

To enable MSSQL databases in your project you must copy/add System.Data.dll to your asset folder.

You can find System.Data.dll here: C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\unity

Once added, you must add it to your references inside MonoDevelop or Visual Studio.

Add usings:

 using System.Data;
 using System.Data.SqlClient;

Now you can connect to your database:

 private string _conString = @"Data Source = 127.0.0.1; 
     user id = Username;
     password = Password;
     Initial Catalog = DatabaseName;";

 public string SimpleQuery ( string _query ) {
     using (SqlConnection dbCon = new SqlConnection(_conString)) {
         SqlCommand cmd = new SqlCommand(_query, dbCon);
         try {
             dbCon.Open();
             string _returnQuery = (string) cmd.ExecuteScalar();
             return _returnQuery;
         }
         catch (SqlException _exception) {
             Debug.LogWarning(_exception.ToString());
             return null;
         }
     }
 }

Then you can use something like this.

 SimpleQuery("SELECT TOP 1 ColumnName FROM TableName WHERE ColumnName2 = 'SomeValue'"); 

Hope this will help, took me a while to gather all the fragmented information. Not many people use MSSQL databases, but in my situation it worked out for the best.

This question has been wikified.

Comment
emauch
Thiadeon
kowailiux
lexen1
federicoagustincaccia

People who like this

5 Show 4
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 RyanFaeScotland · Mar 31, 2015 at 08:40 AM 0
Share

Although certainly useful information I don't think this qualifies as a question and hence doesn't belong here.

avatar image jamal-dahbur · Oct 10, 2016 at 01:01 AM 0
Share

This is working amazingly in the editor and on Android however on iOS it is constantly crashing when trying db.Open(), is there something missing? Please help :(

avatar image jamal-dahbur · Oct 10, 2016 at 01:01 AM 0
Share

This is working amazingly in the editor and on Android however on iOS it is constantly crashing when trying db.Open(), is there something missing? Please help :(

avatar image emauch · Nov 22, 2016 at 08:15 PM 0
Share

It isn't a question, but it was exactly what I was looking for. Wish all "Questions" were this easy to find :)

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Vitaly-Abilevich · Aug 16, 2017 at 03:21 PM

I am getting this error after enabling TCP/IP connection:

System.Data.SqlClient.SqlException: Server does not exist or connection refused. ---> Mono.Data.Tds.Protocol.TdsInternalException: Server does not exist or connection refused. ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.

at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in :0 at System.Net.Sockets.Socket+Worker.Connect () [0x00000] in :0 --- End of inner exception stack trace --- at Mono.Data.Tds.Protocol.TdsComm..ctor (System.String dataSource, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion tdsVersion) [0x00000] in :0 at Mono.Data.Tds.Protocol.Tds..ctor (System.String dataSource, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion tdsVersion) [0x00000] in :0 at Mono.Data.Tds.Protocol.Tds70..ctor (System.String server, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion version) [0x00000] in :0 at Mono.Data.Tds.Protocol.Tds80..ctor (System.String server, Int32 port, Int32 packetSize, Int32 timeout) [0x00000] in :0 at Mono.Data.Tds.Protocol.TdsConnectionPoolManager.CreateConnection (Mono.Data.Tds.Protocol.TdsConnectionInfo info) [0x00000] in :0 at Mono.Data.Tds.Protocol.TdsConnectionPool.GetConnection () [0x00000] in :0 at System.Data.SqlClient.SqlConnection.Open () [0x00000] in :0 --- End of inner exception stack trace --- at System.Data.SqlClient.SqlConnection.Open () [0x00000] in :0 at GUIManager.SimpleQuery (System.String _query) [0x00010] in C:\Users\Rusoski\Documents\Virtual Warehouse\Assets\VirtualWarehouse\Scripts\GUI\GUIManager.cs:403 UnityEngine.Debug:Log(Object) GUIManager:SimpleQuery(String) (at Assets/VirtualWarehouse/Scripts/GUI/GUIManager.cs:410) GUIManager:Update() (at Assets/VirtualWarehouse/Scripts/GUI/GUIManager.cs:295)

Comment
Bunny83

People who like this

-1 Show 1 · 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 shockelite · Aug 16, 2017 at 05:08 PM 0
Share

I would attempt a connection outside of your Unity project. If it still doesn't work it could be a firewall issue. Additionally you can add "Connection Timeout=1" to your connection string so it times out faster. Useful for testing.

avatar image

Answer by JustinJeon · Jun 21, 2018 at 04:42 AM

I don't know this reply is too late. but I'd like to tell you this..

I have same issue.

This problem caused by computer name.

If your computer name is English, It's ok.

But, If your computer name is non-english like korean, chinese and japanese,

then you have that problem.

No body know this issue. I think there is no solution.

Just run again after changing computer name as english.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Distribute terrain in zones 3 Answers

SQL Connection doesn't work in standalone/build 1 Answer

How to fix losing System.Data.dll in Project? 2 Answers

Multiple Cars not working 1 Answer

Oledb and Odbc missing in System.Data 0 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