Hi Everyone,
I’ve been struggling to connect to a local instance of SqlExpress. I’ve read through the various postings and tried a number of things, but still can’t connect. I suspect the problem is the connection string I’m using. I understand that the connection string Mono requires is different than the one MS .NET requires. Using the Mono version and running my code in MonoDevelop, I’m able to connect just fine. However, the same code, when run in Unity, fails to connect.
I’ve seen code posted in the forums which the authors say works (in fact, the code I’m using is a cut-and-paste from one of the postings). The only difference of significance that I can see is that I’m trying to access SqlExpress, which typically requires “\sqlexpress” after the hostname in Server. Perhaps Unity doesn’t like SqlExpress for some reason???
Here’s what my personal trace is showing (it prints out the connection string I’m using, followed by the exception message).
Connecting to the database(Server=string\sqlexpress;Database=AmksPhaseII;User ID=string\barry;Password=ttpp1357.;Integrated Security=SSPI)
Exception: An existing connection was forcibly closed by the remote host.
Here’s my complete code, as it appears in Unity. What am I doing wrong???
public class Test2
{
public void Run(Logger l)
{
string connectionString =
“Server=string\sqlexpress;” +
“Database=AmksPhaseII;” +
“User ID=string\barry;” +
“Password=pswd;” +
“Integrated Security=SSPI”;
l.Log(“Connecting to the database(” + connectionString + “)”);
Console.WriteLine(“Connecting to the database(” + connectionString + “)”);
//Console.WriteLine (“Connecting to the database({0})”, connectionString);
try
{
IDbConnection dbcon;
using (dbcon = new SqlConnection(connectionString)) {
dbcon.Open();
using (IDbCommand dbcmd = dbcon.CreateCommand()) {
string sql =
"SELECT BinName " +
“FROM Bin”;
dbcmd.CommandText = sql;
using (IDataReader reader = dbcmd.ExecuteReader()) {
while(reader.Read()) {
string BinName = (string) reader[“BinName”];
l.Log("Name: " + BinName);
Console.WriteLine("Name: " + BinName);
}
}
}
}
}
catch (Exception ex)
{
l.Log("Exception: " + ex.Message);
Console.WriteLine("Exception: " + ex.Message);
}
Console.ReadLine();
}
}
Thank you for any help!
Barry