-1

I have a WPF application with Entity Framework connected to SQL Server 2017. I'm going to give my client a trial/demo version for testing. Since I don't want to overwhelm him with installing a full SQL Server right now, I'm adding the .mdf database to the root directory and trying to use the SQL Server LocalDB solution to hook things up.

So I deploy the software using Advanced Installer and add all the prerequisites. When I install it in the machine that I'm coding in, the program works fine, but as soon as I try other PCs it doesn't go nowhere. There is a log in window at the beginning where after putting the credential, the window either disappear or in some cases becomes not responding.

I tried a bunch of different types of Connection strings, but no luck so far. Here is the main connection string:

<add name="IncomsDatabase" 
     connectionString="Server=(LocalDB)\MSSQLLocalDB; Integrated Security=true ;AttachDbFileName=|DataDirectory|\Incoms.mdf" 
     providerName="System.Data.SqlClient"/>

I even changed the |DataDirectory| to the direct location of the root folder but it didn't work either.

I also tried different versions of SQL Server LocalDB like, 2017, 2016 and 2014 but without success.

It would be really appreciated if somebody can come up with a workaround.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
msnKh
  • 1

1 Answers1

0

There are two things most likely to have gone wrong:

1) Instance name. Localdb has an instance name just like "full" sql server and maybe it isn't the default. You can check the instances installed using

SqlLocalDB.exe i

This will be installed in:

 C:\Program Files\Microsoft SQL Server\nnn\Tools\Binn

Where nnn is the sql server version number. It'll be like 120 or some such.

You can also get info about which databases are associated with a specific instance. eg

SqlLocalDB.exe i "MSSQLLocalDB"

The mdf location.

A user cannot write to a file ends up in program files. When you work on an app, the code probably isn't in program files. A database which ends up alongside your exe in your bin will work fine. When you install an app, it might be your mdf is somewhere under program files and the user will not be able to connect to a database which they can't write to.

DataDirectory should be in appdata.

Double check what that is set to.

You should have something like:

AppDomain.CurrentDomain.SetData("DataDirectory", uri);

Where uri is built using the path to appdata using one of the Environment.SpecialFolder options such as LocalApplicationData. The ApplicationData one refers to roaming.

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));

You will also want a folder for your application, maybe within another folder which is your company name.

It's possible the client pc doesn't have roaming appdata or that this is reset each log on.

I would check your install actually copies the mdf to the expected folder when you run it.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • Thanks for the reply. it turned out that the localDB had not been installed/started properly. I deleted the instance, added again and started and it worked fine. – msnKh Jul 25 '20 at 08:23