none
How to deploy/publish Windows Form (WinForm) application with its database RRS feed

  • Pergunta

  • I just finished building a C# WinForms application, and after publishing it using Microsoft Visual Studio Installer projects, I have realized that the database has not been added to the application...

    How can I attach the database along with the .EXE file? I have selected the needed prerequisites (.NET and SQL Server Express).

    I am using Entity Framework 6 (EF6) and my database has a username and password (SQL Server authentication).

    I have tried to include the .mdf file in the project, but no results.

    I have also tried to include SQL(localDB), but it throws an exception: make sure to include entity framework connection string...


    • Editado Moosa Arbeed terça-feira, 19 de novembro de 2019 12:12
    • Movido CoolDadTx quarta-feira, 20 de novembro de 2019 14:56 Setup related
    terça-feira, 19 de novembro de 2019 12:10

Todas as Respostas

  • Hello,

    The following free Microsoft installer allows adding your database to an installation.

    See the following for EF on client machine (it does not need to be installed)

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/2f5dc5e1-093a-44ba-985c-2c3157eac8ac/do-i-need-install-entity-framework-on-client-site?forum=wcf


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    terça-feira, 19 de novembro de 2019 13:13
  • You can encrypt and decrypt a connection string in app.config via the following class

    using System;
    using System.Configuration;
    using System.IO;
    public class ConnectionProtection
    {
        public string FileName { get; set; }
        /// <summary>
        /// Determine if configuration file exists for application
        /// </summary>
        /// <param name="fileName">Current executable and path</param>
        public ConnectionProtection(string fileName)
        {
            if (!File.Exists(string.Concat(fileName, ".config")))
            {
                throw new FileNotFoundException(string.Concat(fileName, ".config"));
            }
            this.FileName = fileName;
        }
        /// <summary>
        /// Encrypt ConnectionString
        /// </summary>
        /// <param name="encrypt"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private bool EncryptConnectionString(bool encrypt, string fileName)
        {
            bool success = true;
            Configuration configuration = null;
    
            try
            {
                configuration = ConfigurationManager.OpenExeConfiguration(fileName);
                var configSection = configuration.GetSection("connectionStrings") as ConnectionStringsSection;
    
                if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
                {
                    if (encrypt && (!configSection.SectionInformation.IsProtected))
                    {
                        // encrypt the file
                        configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    }
    
                    if ((!encrypt) && configSection.SectionInformation.IsProtected) //encrypt is true so encrypt
                    {
                        // decrypt the file. 
                        configSection.SectionInformation.UnprotectSection();
                    }
    
                    configSection.SectionInformation.ForceSave = true;
                    configuration.Save();
    
                    success = true;
    
                }
            }
            catch (Exception)
            {
                success = false;
            }
    
            return success;
    
        }
        public bool IsProtected()
        {
            var configuration = ConfigurationManager.OpenExeConfiguration(FileName);
            var configSection = configuration.GetSection("connectionStrings") as ConnectionStringsSection;
            return configSection.SectionInformation.IsProtected;
        }
        public bool EncryptFile()
        {
            return File.Exists(FileName) && EncryptConnectionString(true, FileName);
        }
        public bool DecryptFile()
        {
            return File.Exists(FileName) && EncryptConnectionString(false, FileName);
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    terça-feira, 19 de novembro de 2019 13:21
  • Hi

    As I have mention in my question, I am already using Microsoft Visual Studio Installer Projects.

    And of course Entity Framework will not be installed in client machine (common sense).

    My question was how to deploy the Application in client machine with its database, I have used EF6 for developing this app.

    terça-feira, 19 de novembro de 2019 14:29
  • why do I need to encrypt and decrypt??
    terça-feira, 19 de novembro de 2019 14:30
  • why do I need to encrypt and decrypt??

    Never mind I missed the part about using SQL-Server authentication.

    In regards to the installer, may who ask when indicating the Installer project tend to mean ClickOnce.

    See the following docs for including a database.

    Here is an example I did for one of my projects using the Installer.

    Right click on the project to get


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    terça-feira, 19 de novembro de 2019 17:34
  • here are the details and the connection string that I am using

    terça-feira, 26 de novembro de 2019 09:30
  • and this is the exception that I am getting

    terça-feira, 26 de novembro de 2019 09:30
  • Hi Moosa Arbeed,
    After you select SQL Express in prerequisites, you should copy your database files and add to the output directory. Then use AttachDbFileName in connection string: “Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Database.mdf; Initial Catalog=Master"
    More details you can refer to Reza Aghaei’s answer.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    quinta-feira, 28 de novembro de 2019 09:30