0

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.

Bilal
  • 558
  • 8
  • 18
  • What is the error message? – Steve May 05 '17 at 21:23
  • @Steve It's quite long but I ll try to put important part.. A network related or instance specific error occured while establishing a connection to SQL Server.The server was not found or accessible. Verify that hte instance name is correct and that SQL Server is configured to allow remote connections.(provider: SQL Network Interfaces,error 50- Localc Databse Runtime error occured. The specified LocalDB instance does not exist. – Bilal May 05 '17 at 21:27
  • Did you install the bits for LocalDB on the failing machine? – Steve May 05 '17 at 21:29
  • @Steve I installed whole package of sql server 2012 express edition on that machine. – Bilal May 05 '17 at 21:31
  • Possible duplicate of [Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?](http://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci) – Filburt May 05 '17 at 21:32
  • LocalDB is a trimmed down version of Sql Server Express. I don't know if installing Sql Server Express you have automatically also installed LocalDB. Better install the LocalDB.msi – Steve May 05 '17 at 21:32
  • It does install local db I checked... it installs all the tools like management studio server explorer n all – Bilal May 05 '17 at 21:34
  • @Filburt he had some db issues with IP . My database is local and present in debug folder of solution. – Bilal May 05 '17 at 21:36
  • If it is installed then you can try this command line from a CMD window: *SqlLocalDb info* you should get a list of all instances installed on that machine – Steve May 05 '17 at 21:37
  • @steve it says v11.0 – Bilal May 05 '17 at 21:39
  • So you don't have the MSSqlLocalDB instance try with _SqlLocalDb c MSSqlLocalDB_ and then try again to connect – Steve May 05 '17 at 21:40
  • @Steve Now error is changed. Now it says database cannot be opened because it is version 852.This server supports 706 and earlier. A downgrade path is not supported. – Bilal May 06 '17 at 06:59
  • This instead is caused by a difference between the Sql Server Version used to build your MDF file on your dev machine and the versione installed in the target PC. Version 852 is Sql Server 2016 while 706 is Sql Server 2012. Of course a previous version of Sql Server cannot read a file created with a following version. The only path I can see is to install the 2016 version on the target machine. – Steve May 06 '17 at 07:37
  • @steve so if I upgrade sql version to 2016 on that machine , it ll work? And which bersion of sql server 2016 will b enough? – Bilal May 06 '17 at 07:38
  • Yes, I think so. LocalDB 2016 is required (unless you deinstall Sql Server 2016 on your dev PC and install 2012, then rebuild your db, but....) – Steve May 06 '17 at 07:40
  • Before installing the new version I would delete the instance created manually. MSSqlLocalDB is the default for 2016 instead of v11.0 of 2012 – Steve May 06 '17 at 07:48
  • You should install the LocalDB 2016 – Steve May 06 '17 at 07:50

1 Answers1

2

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)

Steve
  • 213,761
  • 22
  • 232
  • 286