- Home /
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.
Although certainly useful information I don't think this qualifies as a question and hence doesn't belong here.
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 :(
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 :(
It isn't a question, but it was exactly what I was looking for. Wish all "Questions" were this easy to find :)
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)
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.
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to fix losing System.Data.dll in Project? 2 Answers
SQL Connection doesn't work in standalone/build 1 Answer
Bringing in classes from a DLL 1 Answer