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:
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
BHAVESH DAVEPosted Sep 29, 2010, 4:11 AM
Thank you very much for reply. I tried to understand your point and the code but I get confused due to
1. In book clearly mentioned that use oledb so i have used oledb provider -- access database.
2. "DataAccessLayer.Properties.Settings.VideoGameStoreDB" it is name for connection string of xml file (app.config)
ConnectionStringSettings theConnectionString = ConfigurationManager.ConnectionStrings["DataAccessLayer.Properties.Settings.VideoGameStoreDB"];
You should mention that "DataAccessLayer.Properties.Settings.VideoGameStoreDB" is name in xml file.
that reason I got confused.
Thank you.
Abhimanyu K VatsaPosted Jun 5, 2010, 8:11 AM
If you are trying to run your project on your system while your database is on remote server system then it will have different accessing technique. Otherwise your coding is fine.
Good Luck