1

I make a windows form application with its database and I want to publish it . the app is running in my system so good but when I run it in another PC its shows errors about cannot connect to its database. also I put database mdf and ldf file in program folder but my app doesn't work.

and this is my connection string :

string connectionSt = "Data Source=DESKTOP-NLKJ55F;Initial Catalog=IndustryContracts;Integrated Security=True";
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Feri
  • 11
  • 5
  • 1
    What kind of database are you using? Did you make the mistake of using a **server** database like Sql Server when you should have used an **in-process** database like Sqlite or Access? – Joel Coehoorn Sep 21 '20 at 14:21
  • I used sql server – Feri Sep 21 '20 at 14:22
  • Sql Server is designed to support many users at a time. It wants to run all the time in the background on a dedicated machine, using lots of system RAM to cache data, even when your app is not running. It has a complicated deployment that needs administrator permissions and involves setting up security and connection properties. It's a _really poor choice_ for a local database in a forms app. – Joel Coehoorn Sep 21 '20 at 14:24
  • Most of the time, you don't publish a database (unless it's for static look-ups, etc.). Instead, I'd recommend putting the connection string in both the dev and target machine's environment (such as .NET Core User Secrets or in an environment variable) and having the database already in the location that you need it. If the database schema changes, you can run a migration on it (either as a manual script or as an EF migration.) – Merkle Groot Sep 21 '20 at 14:26
  • What is connection string. Is mdf file attached to a SQL Server or are you using (localDb)AttachDatabase? Local Db is using a default folder for the data base.would look likethis : AttachDBFilename=c:\asd\qwe\mydbfile.mdf. Local database is meant hwne you do not have a SQL Server and the database is small. Do not use both Server=myServerAddress; and AttachDBFilename at the same time. – jdweng Sep 21 '20 at 16:16
  • I use a mdf file as local database. – Feri Sep 22 '20 at 13:17

1 Answers1

2

This is a common mistake for people learning how to develop apps with databases.

Basically: You have the wrong approach. A database is not really meant to be created at every computer where a program is installed. Rather, the ideal use case for a database is one which is created in a server, and any app that requires to use it will simply connect to it, by some protocol like TCP. This helps with encapsulation and also makes your application more compact.

So normally when the program needs to read and write data to be saved somewhere, people usually go with methods such as writing into a CSV file, or a tab delimited file, and then reading from those files.

There is also another option, which is to use a serverless DB such as SQLite, however, this would mean you need to learn how to implement said database.

Lastly, as a really discouraged way to do it, and a bit of a "brute force" approach, you can copy the database creation script, install a DBMS in your target computer, run the script to create the DB, then change the connection string of the DB in your program to match the newly created instance, then install the program. See how extremely clunky that was?

Remember: Most databases run in a server, even if that server is your PC, hence why they're referred to as instances. Naturally a server shouldn't be replicated every time a program is installed.

  • 2
    I disagree with the first part. Databases are **GREAT** for storing local data. You just need the _right kind_ of database. Namely, you want an _in-process_ database like Sqlite or even --gasp-- MS Access (because the Access engine is already available on Windows). Just avoid the server products like Sql Server, MySql, Oracle, Postgresql, etc for this role. – Joel Coehoorn Sep 21 '20 at 14:30
  • you mean there is no way to solve my problem without change my database ? – Feri Sep 21 '20 at 14:37
  • 1
    Unless you want to that horrendous Option 3 I described, yeah that's what I mean. The database you chose is Server-based and it's not appropriate for your use case. –  Sep 21 '20 at 17:50