简体   繁体   中英

Connecting to local SQL Server database file

I have a WPF application connecting to a local SQL Server database file ( .mdf ). I am using Entity Framework.

Connection string is:

Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\db_SmartPOS.mdf;Integrated Security=True

The directory is changed to the following:

string doc = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
AppDomain.CurrentDomain.SetData("DataDirectory", doc + @"\SmartPOS\");

I have copied the .mdf files manually to that specified directory, tested it, and everything is working well on my main machine, where SQL Server 2014 is installed.

Now I am deploying the app to another PC, where I installed SQLLocalDB . However, whenever I try to connect to the database, I get this exception:

System.Data.EntityException. The underlying provider failed to open --> Sysmte.Data.SqlClient.SqlException.....The specified local db instance does not exist.

The .mdf and log files are placed in the directory specified above. I have also tried to copy them to the directory where the application is installed.

I am wondering what am I missing?

So here how it worked.

First of all I found an article online that specified that the connection string in SQL 2014 is different than the one in SQL 2012, therefore Data Source=(LocalDB)\\MSSQLLocalDB used in 2014 should be switched to Data Source=(LocalDB)\\v11.0. If using Entity framework, this should be done the connection strings settings and in app.config.

Once this was done, i tested it on the other system, and the exception error changed. This time the application was able to connect, however there was something wrong with the versions and i was unable to access it.

I downloaded SQL server 2014 localdb engine and replaced the 2012, and everything worked fine!

Hopefully this will help someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM