How to run a C# WinForms program with local database .mdf on another computer

Frączek Rafał 0 Reputation points


I created an application using Winforms and C#. To store various data of application, I use a database. In the application folder, I have a .mdf file called PricesDatabase.mdf.

I have added connection string to app.config:

	<add name="MyConnectionString" 
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\PricesDatabase.mdf;Integrated Security=True;Connect Timeout=30"/>

My connection string looks like this:

string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

On my local PC everything is working correctly, but when I try to copy application to another PC and run app I got error:
'The database 'dbname' cannot be opened because its version 904. This server supports version 852 and earlier. A downgrade path is not supported.

Could not open new database 'dbname' CREATE DATABASE is aborted.

An attempt to attach an auto-named database for file 'dbname' failed. A database with the same name exist or specified file cannot be opened'

I have used Microsoft Visual Studio Installer Projects to create MSI and exe files. Additionaly in Properties Prerequisites I have added SQL Server 2019 Express Local DB. When I uninstal old SQL Servers it helps, previous error disappear but now I got another error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.


SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
11,004 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,015 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 88,546 Reputation points

    You get that error because that other computer has an older version of SQL Server installed. I don't recall exactly, but I believe that 852 is SQL 2017.

    And if there is no SQL Server instance all, of course nothing will work.

    How to deal with this situation, depends on the nature of the application. For a big multi-user application, you assume that SQL Server is installed somewhere on the network, and you only document the required version. (And the application permits the user to select which server to use.)

    For a simple single-user application, that is not very convenient. Rather you should include SQL Server in your install. And if it is a true single-user application, LocalDB is a good choice.

    However, rather than using AttachDBFilename, I think it is better to distribute a backup of the database and restore it to some location when you install LocalDB, so that the database is place once for all.

  2. Javier Villegas 635 Reputation points MVP

    I will suggest you to use that mdf file you have and attach it to the remote instance instead of using AttachDBFilename in the connection string.

    the main problem you have now it that mdf was created on SQL Server 2019 (904) and according to the error message it requieres SQL Server 2016 (852).

    0 comments No comments