Home > database >  Failed to detect local database from client PC in my C# Windows application
Failed to detect local database from client PC in my C# Windows application

Time:09-27

I have a simple Winforms app (C#) in development it works perfectly on my laptop, and I am just trying to test to see if I can publish it for others to use.

It uses a local Microsoft SQL Server database file (SqlClient). I tried publishing what I have (so far) to my friend's PC. When he opened the app, this error appeared and he couldn't access to local database.

This is the app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="sell.Properties.Settings.DatabaseConnectionString"
             connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>

This is the error:

enter image description here

I use the local database like this:

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sell.Properties.Settings.DatabaseConnectionString"].ConnectionString))

Any solutions to fix the problem?

CodePudding user response:

Your connection string specifies this: Data Source=(LocalDB)\MSSQLLocalDB;. If you have installed SQL Server Express, rather than just LocalDB, then that's not going to work. You need to change the data source to refer to the SQL Server Express instance on the local machine, e.g. Data Source=.\SQLEXPRESS;.

  • Related