简体   繁体   中英

Publish C# Desktop Application with SQL Server database

I have a Visual Studio project that has different Windows Forms and a SQL Server database attached. I want to install this project on my client computer (others). As I haven't published any project before, can someone guide me in an easy way.

When I tried to publish (installed) this project in my computer (setup file would be created and the application would be installed but) but I get the error shown below while saving update delete data. Please guide me correctly.

在此处输入图片说明

You are taking a completely wrong approach here.

Databases are not meant to be installed in client's computer. If you have a client/server app, the database should be located only in your server, and the client will access it/request to write into it by sending requests over a protocol like TCP, which your server will react to.

When you add a database to an app, you provide a connection string and this connection string is related to YOUR instance of the database. If you wanted every client to use a unique DB, you'd need to go over to their computers, install a DBMS, create the database there, then edit the program code to include the connection string for the specific client instance. Obviously this is not a viable option unless the clients you're talking about are just like your immediate family/roommates.

If you intend to provide your clients app with saving/reading data functionality, you'd need to implement it by having the data written into a local file which the program will be able to read from, such as a csv or a tab delimited file. Not through a database. At least not SQL server. If you're completely obligated to use a DB, then think about an embedded, serverless DB like one from SQLite

The reason you're getting that error is because the client's program is trying to look for the database instance that is indicated in the code, but of course, this instance does not exist in the client's computer, so the client cannot reach it to do anything.

Also, please remember to use exception handling. We cannot tell what the exact error was (although it is obvious) because your code is only saying "an unhandled exception happened". Always wrap your code In try/catch blocks and at the very least have them print the exception to a console so that you can know WHY did a program failed on runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM