*** SOLVED ***
The request below has been solved by simply replacing the database name with the IP Address. For example:
DatabaseSever\DatabaseName:\DatabaseInstance needs changing to
IPAddress\DatabaseName\DatabaseInstance
Hi all,
I’ve seen other similar questions and feel like I have everything setup correctly, save one config option.
Using C# code from my VisualStudio project which connects to my database correctly, I am getting the following error when executing the request in Unity:
System.Data.SqlClient.SqlException: SQL Server does not exist or access denied.
I have ensured that System.Data.dll resides in the correct project folder and have also enabled TCP/IP on the database.
Can anybody help with me the last part to this puzzle?
The code I am using is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using UnityEngine.UI;
using UnityEngine;
namespace DatabaseConnectionTest
{
public class SQLFunctions : MonoBehaviour
{
public InputField inputDBInstance;
public InputField username;
public InputField password;
public Text connectionStatus;
public static bool RemoteDBConnection = false;
void Start()
{
inputDBInstance.text = "";
connectionStatus.text = "No";
}
static SqlDataAdapter TableDataFetcher;
static DataTable SQLDataTable;
public static SqlConnection AppConnection = new SqlConnection();
static SqlCommand SQLCommand = new SqlCommand();
static UnityEngine.Random RandomNumber = new UnityEngine.Random();
private static readonly UnityEngine.Random RandomBase = new UnityEngine.Random();
public static void FConnectToDatabase(string DBServer, string SQLServerInstance, string Database, string UserName, string Password)
{
AppConnection = new SqlConnection(string.Format(@"Data Source={0}\{1}; Initial Catalog={2}; User id={3}; Password={4};", DBServer + (RemoteDBConnection == true ? ".ksdl.local" : ""),"Initial Catalogue=" + SQLServerInstance, Database, UserName, Password));
if (AppConnection == null)
{
AppConnection = new SqlConnection() { ConnectionString = "INVALID" };
}
}
public static void FOpenConnection(SqlConnection ConnectionString)
{
try
{
if (ConnectionString.ConnectionString == "INVALID")
{
throw new Exception();
}
if (ConnectionString.State == ConnectionState.Closed)
{
ConnectionString.Open();
}
}
catch (Exception ex)
{
FException(ex);
}
}
public static void FCloseConnection(SqlConnection ConnectionString)
{
try
{
if (ConnectionString.State == ConnectionState.Open)
{
ConnectionString.Close();
}
}
catch (Exception ex)
{
FException(ex);
}
}
public static void FGetStockTypes(ref DataTable Table, SqlConnection Connection)
{
try
{
TableDataFetcher = new SqlDataAdapter("SELECT SHORT_DESC FROM stock_type", Connection);
TableDataFetcher.Fill(Table);
}
catch (Exception ex)
{
FException(ex);
}
}
public static void FException(string ExceptionMessage, string ExceptionTitle = "ERROR!")
{
//MessageBox.Show(ExceptionMessage, ExceptionTitle, Buttons);
Debug.Log("Error");
}
public static void FException(Exception ex, string ExceptionTitle = "ERROR!")
{
if (ex.InnerException != null && ex.InnerException.InnerException != null)
{
Debug.Log(ex);
}
else if (ex.InnerException != null && ex.InnerException.Message != null)
{
Debug.Log(ex);
}
else
{
Debug.Log(ex);
}
}
public void OnDatabaseConnectButton_Click()
{
string[] ConnectionStringElements = inputDBInstance.text.Split(new string[] { "\\", ":" }, StringSplitOptions.RemoveEmptyEntries);
if (ConnectionStringElements.Length != 3)
{
Debug.LogError("Invalid Connection string entered!");
}
else
{
FConnectToDatabase(ConnectionStringElements[0], ConnectionStringElements[1], ConnectionStringElements[2], username.text, password.text);
FOpenConnection(SQLFunctions.AppConnection);
if (AppConnection.State == ConnectionState.Open)
{
connectionStatus.text = "Connection Successful";
FCloseConnection(AppConnection);
}
}
}
}
}