Connecting to localdb with Visual Studio and NUnit

796 views Asked by At

I'm trying to run unit tests out of NUnit that are centered around databases. The tests just access the localdb but NUnit keeps giving errors. I've set up a localdb (or so I think), but the sql command isn't recognizing it.

Exception occurred while deleting from table: 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: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.

Here is the path for the sql command in the unit test:

private const string ConnectionString_ = "Data Source=(localdb)\\Projects;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";

I've read over most of the related sql questions and I'm currently reinstalling the sql server. No one seems to be getting the same error. I suspect I've missed something very simple.

2

There are 2 answers

0
Rodney Cabahug On

I think you should change the Data Source to (localdb)\v11.0. If you are using VS2015, you can use (localdb)\MSSQLLocalDB as Data Source. So in your case it should be the following.

private const string ConnectionString_ = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";

or

private const string ConnectionString_ = "Data Source=(localdb)\\v11.0;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";
0
Martin On

The connection string does not look right. I do not see the "AttachDbFilename" part which points to the mdf file which represents the local db.

Try this:

connectionString=@"Data Source=(LocalDb)\v11.0;AttachDbFilename=PATH_TO_MDF.mdf;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";

Or see this response to a similar problem.