MS SQL Server 2008 + Unity3D connection

I’m having trouble connection the MS Sql Server database to Unity3D.
Here is the error I’m getting:

SocketException: No connection could be made because the target machine actively refused it.

System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy)
System.Net.Sockets.Socket+Worker.Connect ()
Rethrow as TdsInternalException: Server does not exist or connection refused.
Mono.Data.Tds.Protocol.TdsComm..ctor (System.String dataSource, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion tdsVersion)
Mono.Data.Tds.Protocol.Tds..ctor (System.String dataSource, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion tdsVersion)
Mono.Data.Tds.Protocol.Tds70..ctor (System.String server, Int32 port, Int32 packetSize, Int32 timeout, TdsVersion version)
Mono.Data.Tds.Protocol.Tds80..ctor (System.String server, Int32 port, Int32 packetSize, Int32 timeout)
Mono.Data.Tds.Protocol.TdsConnectionPoolManager.CreateConnection (Mono.Data.Tds.Protocol.TdsConnectionInfo info)
Mono.Data.Tds.Protocol.TdsConnectionPool.GetConnection ()
System.Data.SqlClient.SqlConnection.Open ()
Rethrow as SqlException: Server does not exist or connection refused.
System.Data.SqlClient.SqlConnection.Open ()
(wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlConnection:Open ()
dbConn.Start () (at Assets/Scripts/dbConn.cs:55)

An here is what the C# code looks like:

using UnityEngine;
using System.Collections;
using System;
using System.Data;
using System.Data.SqlClient;

class dbConn : MonoBehaviour

    public string boat;

	static string connectionString =
          "Server=localhost,1433;" +
          "Database=database;" +
          "User ID=user;" +
    static SqlConnection cn = new SqlConnection(connectionString); 

    static string sCommand = "SELECT * FROM dbo.Store";

    static SqlDataAdapter da = new SqlDataAdapter(sCommand, cn);

    static DataTable dataTable = new DataTable();

    void Start()

        int recordsAffected = da.Fill(dataTable);
        if (recordsAffected > 0) 
            foreach (DataRow dr in dataTable.Rows)
                store = dr["storename"].ToString();
    void OnGUI() 
        GUI.Label (new Rect (10, 10, 200, 20), "Store: "+store);

I’ve check the firewall, and it was not the issue. I’m using SQL Server Authentication and not Windows Authentication

  1. Try creating a connection using the ODBC tools to verify your connection string.
  2. Use SQL Server Management Studio and make a connection to the same server address using the same user name and password and see if you can see the database and tables that you are looking for.

The errors you have there direct to a connection problem between the application and the database server. Check the firewall, check connections and permissions. It could be as simple as the TCP Protocal inside the sql connection manager is not enabled.

This is the connection string i use:

private string connectionString = "Data Source=\\SQLEXPRESS;Initial Catalog=Demo_Phoenix;Persist Security Info=True;User ID=**;Password=***;";

I had alot of problems with my connection because i used a database name, not an IP adress. Maybe you can try that?

I met this met this problem, too. The solution is to got SQL server configuration manager->SQL Native Client 11.0 Configuration(32bit)/SQL Native Client 11.0 Configuration → Client Protocols then Enable TCP/IP

SQL databases can be used by using of sql db fix

Explore next entry