Bilal
Bilal

Reputation: 588

Build a universal connection string

I developed a win form based application which involves database. I built the database in visual studio inside that solution. My current connection string in app.config file is:

connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\NARA.mdf;Integrated Security=True;" providerName="System.Data.SqlClient"/>

But to check it I copied my Debug folder which included all files (including database) but it gave connection string exception on my other system. How can I make a connection string which can work on any system I take my program to.

Upvotes: 0

Views: 540

Answers (1)

Steve
Steve

Reputation: 216302

All of your problems are caused by incompatibility between Sql Server versions.
When you install the 2012 LocalDB it has a default instance named v11.0. So, if you want to connect to this default instance and let it manage your MDF file you need a connection string with

connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=....."/>

or you can create an instance with the name required by your connection using an administrative command prompt and typing

SqlLocalDb c MSSqlLocalDB 

But this is not the end of the problems, because now you need to have an MDF file created with Sql Server 2012 version (localdb or not). And, as clear from the error message received, you have an MDF file created with Sql Server 2016.

The 2012 cannot read file created with 2016. This has always been the case with Sql Server (and logically so) because new versions of the MDF file always contains enhancements or internal changes that create this incompatibility.

The fix is simple: Install a version of your LocalDB bits that are compatible with the MDF file you distribute. (Remember that LocalDB is a developper facility and shouldn't be used in production albeit in simple scenario it can works, for example I use it to distribute a demo of my full scale application. So a customer with a poor IT department can evaluate the product without being forced to install the Sql Server Express o the full version)

Upvotes: 2

Related Questions