Search code examples
c#winformspublish3-tier

How do I publish a 3-tier WinForms application with database (.mdf)?


Scenario

I am working on a 3-tier Winforms application that has a database.MDF file in the database layer and I have linked all layers and ready to be published. I have a connection string that connects to the local DB.

Question

How can I publish a 3-tier architecture application with a database with setup?

My attempt

I have tried publishing UI Layer as it is connected with all other layers. After seeing the installation folder I don't see any database file with that.

I have gone through Youtube videos and some blogs, nothing seems to work.

Thanks in advance!


Solution

  • I think this became simple when I used Advanced Installer,

    First of all, attaching the Local DB to your project needs this type of connection string,

    string path = Path.GetFullPath(Environment.CurrentDirectory);

    string databaseName = "Your Database Name.mdf";

    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + path + @"\" + databaseName + "Trusted Connection = True");

    This type of connection makes the app connect to the database where ever the app gets installed.

    while creating the installer make sure to copy and paste the .mdf files to the files of the installer as the advanced installer omits the .mdf files.

    After attaching the .mdf files make sure to provide full control privilege to the user who installs it.

    Tested this method, works fine!