DB connection fails (System.Data.Sqlclient.SqlException)

SetFlame picture SetFlame · Oct 3, 2013 · Viewed 9.2k times · Source

I am programming a Weatherstation which gets the data from an API. This works fine, but I want to store it in a database to draw a graph with it. But my code does not seem to work. I tried debugging it and it fails on the line where I want to open the database. It runs through the line where I create the DB.

These are the failing lines:

databaseCon = new SqlConnection(@"Data Source=
(LocalDB)\v10.0;AttachDbFilename=C:\Users\Jeroen
Laptop\Desktop\Eindopdrachten\WeatherStation\Eindopdracht\
Weather.mdf;Integrated Security=True;");
databaseCon.Open();
        

Error message in the console:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

What happens:

I want to get the data from the API and put this into the DB for making a graph with temperatures, location and date. For this I created a database and I want to connect with it. This is where the error kicks in. I get the error from above. When I debug that message shows up a few times(100 or so) and continues to run, without filling the database.

I hope someone here can give me some more information or can see what I do wrong.

Update:

I got the following message when trying to acces the database

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: 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: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)

I use Visual Studio 2010 with SQL server 2008. 2012 is not supported by my programming laptop.

Answer

Novaterata picture Novaterata · Jan 27, 2014

It must be v11.0, not v10.0, and in the future there will be v12.0+, but there is no v10.0. This has nothing to do with Visual Studio 2010 or SQL Server 2008. LocalDb first came with SQL Express 2012 and is v11.0. http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx

LocalDb also required as least .NET 4.02, but unfortunately the version in the registry is always v4.0.30319, even if you have earlier .NET http://blogs.msdn.com/b/sqlexpress/archive/2011/10/27/net-framework-4-now-supports-localdb.aspx

If possible, just upgrade to .NET 4.5

This problem can also exist if using a named instance instead of v11.0 and the program or service was ran as a different user and that user instance is still attached to the database. Unfortunately the database can take awhile to automatically detach.