ConfigurationManager/TK70561
Hello,
I am starting to work through the test kit for 70-561 (ado.net) and I am having a hell of a time getting this right. If I set this up in a console app everything works and I get a connection to my database.
However, if I set everything up in a class library and try running a unit test, I get this SQLException:
Assert.AreEqual failed. Expected:<Database Connection is Open>. Actual:<A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)>.
This is what confuses me, the EXACT same code in a console application connects to the DB no problem. Do I have to do anything differently in a class library to get it to work?
Here's the code in question:
string returnMessage = String.Empty;
try
{
ConnectionStringSettings theConnectionString = ConfigurationManager.ConnectionStrings["DataAccessLayer.Properties.Settings.VideoGameStoreDB"];
SqlConnection theConnection = new SqlConnection(theConnectionString.ConnectionString);
theConnection.Open();
if (theConnection.State == System.Data.ConnectionState.Open)
returnMessage = "Database Connection is Open";
}
catch (SqlException sqlexception)
{
returnMessage = sqlexception.Message;
}
catch (Exception exception)
{
returnMessage = exception.Message;
}
return returnMessage;
}
Cheers,
Dan