1

I am creating a POS system in Windows Forms (C#) in which I use a SQL Server database file (.mdf) to store items (completely offline). When I install the application on my computer, it works fine, but when I install it on my clients PC, an error happens:

(provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation"

I read somewhere that the problem is caused due to the fact that the connection string of the database of my client's PC is different. I tried to add the connection string dynamically on runtime but again it only worked on my computer.

Another reason that might be causing the problem is that I used 'server-based database' since local database option isn't available in Visual Studio 2017 for some reason.

Another solution I looked up stated that I should install SQL Server Express on my client's PC. That also failed (maybe I have to set it up in a way or something).

I also tried adding the database.mdf and database_log files in the setup folder.

Lastly I tried installing 3rd party installers (Advanced installers 15.8 and InstallShield Wizard in VS 2015) which also failed.

(I have provided the code for the connection of database taking place and the connection string)

    public void ConnectToDB()
    {
        DBConnection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\SAIM NASSER\Desktop\app layer\data layer\Database1.mdf; Integrated Security = True");

        DBConnection.Open();

        ResultSet = new DataSet();
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Saim Nasser
  • 71
  • 1
  • 6
  • 1
    You're trying to connect to **SQL Server Express LocalDB** - so therefore you **must install** that version on any PC where you want to run your app. – marc_s Apr 12 '19 at 12:58

1 Answers1

3

If I understand you correct, you want to use LocalDB
That means using Sql Server without installing a full sql server, but just the localdb part from sql server express.

For this to work you need to install the LocalDB Driver, which can be found here
https://www.microsoft.com/en-us/download/details.aspx?id=29062

You need only the ENU\x64\SqlLocalDB.MSI

This is the only thing you need to install in your clients computer, I believe it can also be installed silent, you have to research a bit for that.

And yes, you also should change the connection string on the clients computer, you need to alter it so it points to the MDF file on the clients computer, because that location will probably be different then on your computer

EDIT
To get the connection string working, you can try this
On the clients computer, create a text file and rename the extension to .udl
So for example you have a file test.udl
Now from explorer, double click it, this will open the datalink editor.
From here you can enter/choose different settings, and click on the test connection button.
Once you get a working connection, save it, and open this file with notepad.
Inside you will find the working connection string
Hope this helps

GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • @marc_s That is what I am saying, he should install it. His error message leads me to believe that local db is simply not installed – GuidoG Apr 12 '19 at 13:02
  • 1
    But your statement *without installing SQL Server* is misleading - you **still** need to **install** a SQL Server engine in the form of "LocalDB" – marc_s Apr 12 '19 at 13:02
  • 1
    @marc_s Oh that is what you mean, I will try to alter it to make it more clear – GuidoG Apr 12 '19 at 13:03
  • i have downloaded sql server express (in which LocalDB is installed as well)...the application is still giving the exact same error – Saim Nasser Apr 12 '19 at 13:05
  • Then this part `"Data Source = (LocalDB)\MSSQLLocalDB;` of your connectionstring might need to be different. I will edit my answer with a possible solution for that – GuidoG Apr 12 '19 at 13:09
  • Are you also sure you installed the LocalDB too when installing sql server express ? I am not sure if this is installed by default – GuidoG Apr 12 '19 at 13:15
  • yes i have downloaded the LocalDB (confirmed through command prompt).i also tried changing the connection string dynamically on which it now shows the error: "An attempt to attach an auto-named database"... – Saim Nasser Apr 12 '19 at 14:12
  • OK, that means at least that LocalDB is up and running. Now look at [this](https://stackoverflow.com/questions/12566036/an-attempt-to-attach-an-auto-named-database-for-file-database1-mdf-failed) question. There are some answers here that can help with that. – GuidoG Apr 12 '19 at 14:35