0

I need help solving this problem. I created the application installer using MS Visual Studio Installer Projects and ran it on another device. There were no versions of LocalDB that installed on my computer (2016 and 2017) in the prerequisites, so I had to download the SQL Server 2017 LocalDB on another computer manually. After that, when I started the program I received the following error.

Database files were automatically placed during installation in the folder Documents

I changed the connection string as follows:

string dbPathMyDoc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string dbPath = Path.Combine(dbPathMyDoc, "myprojectAppData"); 
AppDomain.CurrentDomain.SetData("DataDirectory", dbPath);

Database path

So it seems to me that the problem is not the connection string, but then what?

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    I don't see anything related to connection here – T.S. May 27 '20 at 20:30
  • @T.S.i mean that i changed path in connection string*, sorry . I use the connection string from the App.config. After executing the code indicated in the question, connection string looks like this: `Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\Dmitriy\Documents\myprojectAppData\myprojectDB.mdf";Integrated Security=True"` – Дмитрий May 27 '20 at 20:49
  • Do you have the file in this directory? Do you have `app.config` or `yourAPP.exe.config`? – T.S. May 27 '20 at 21:51
  • @T.S.on a computer that has a problem connecting to the database? No, this file is located on the following path along with the installed program: C:\Program Files (x86)\Company Name\myproject\myproject.exe.config. The database file is located at the following path: C:\Users\Dmitriy\Documents\myprojectAppData\\myprojectDB.mdf – Дмитрий May 27 '20 at 22:19

2 Answers2

1

I found that I can not check the version of SQL Server LocalDB installed on a second computer through the Command Prompt using the command sqllocaldb info MSSQLLocalDB (because of an error).

So I decided to recreate the MSSQLLocalDB instance using the following commands:

1) sqllocaldb d MSSQLLocalDB

2) sqllocaldb c MSSQLLocalDB

And after that the program successfully connected to the database.

I hope this information helps someone.

  • I had the same problem running my tests on another computer after uninstalling an older version of Visual studio and LocalDb. Uninstalling and reinstalling Visual studio and LocalDb doesn't help. But after recreating the MSSQLLocalDB as you outlined in your answer and starting the instance, I could connect to the database file using server explorer and my unit tests started working again. This seems to be the same problem and solution as described in this article: https://stackoverflow.com/questions/40022742/localdb-parent-instance-version-invalid-mssql13e-localdb – Filip Lindboe Sep 16 '22 at 08:20
0

I make sample fixed problem for all project when create ny winforms & localdb

You can create 2 ConnectionString in App.config file

App.config

<add name="DevConnectionString"
      connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" /> <!--  *1* Use when publish project--> 

<!--<add name="AppConnectionString"
        connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='E:\My Projects\MyDatabase.mdf';Integrated Security=True"
        providerName="System.Data.SqlClient" />-->
<!--*2* Use for development-->
  • First use when you want develop system Developer Environment
  • Second CS use when app publish Publish Environment

For second connection-string must write full path


Code

For get connection String in App.config file, you can use this code

var conStr = ConfigurationManager.ConnectionStrings["DevConnectionString"].ConnectionString;
Zanyar Jalal
  • 1,407
  • 12
  • 29
  • Yes, I already use 2 connection strings, but in a slightly different way. Perhaps you misunderstood my question. I deploy my program with LocalDB on another computer (not on the one where the development was conducted) and after installation, when I start the program I get an error connecting to the database. – Дмитрий May 27 '20 at 22:11
  • You must download prerequisite **SqlServer localdb 2017** – Zanyar Jalal May 28 '20 at 08:34
  • It has already been installed. The problem was with the instance MSSQLLocalDB and after its removal and re-creation problem solved. – Дмитрий May 28 '20 at 11:00