Winform Project Creates Installer with sql database RRS feed

  • Question

  • I have a winform project which uses a sql database, if I want to create an Installer for the project. How to include the mdf file? By the way. I am using project install in VS 2017.
    Sunday, January 7, 2018 5:34 AM

All replies

  • You don't MS SQL Server must be installed separately.
    Sunday, January 7, 2018 6:01 AM
  • Do you want an empty database? Then the thing to do is to create the database, either in the setup or during execution by checking if it does not exist.

    I am not sure what to do if you need to provide data with the application. Perhaps you need to create the database then insert the data from a file or some files in a flat-file format or something like that.

    Please be more specific about the requirements.

    Sam Hobbs

    Sunday, January 7, 2018 11:34 PM
  • Hello,

    You could create the database on the fly using SQL Server Management Objects (SMO). To get an idea how to use SMO see my code sample Working with SQL Server Management Objects Part 3.

    The assemblies are located here.

    Let say I want to create the database in the same folder as the application we specify the path via db.FileGroups, if you want the database created in the default path remove the two lines dealing with FileGroups.

    /// <summary>
    /// Create a new mdf database in the current folder of the executable
    /// along with creating a table with several fields including a primary key
    /// </summary>
    public void CreateMdf()
        var mdfName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Karen.mdf");
        Database db = null;
        Server srv = null;
            srv = new Server();
            db = new Database(srv, "KarensDatabase");
            db.FileGroups.Add(new FileGroup(db, "PRIMARY"));
            db.FileGroups[0].Files.Add(new DataFile(db.FileGroups[0], "KarenPayneFile", mdfName));
        catch (Exception ex)
                * Possible issues, SQL-Server not installed or 
                * the user does not have proper permissions to create
                * objects
        Table tbCustomers;
        tbCustomers = new Table(db, "Customers");
        Column nameColumn;
        nameColumn = new Column(tbCustomers, "Name", DataType.NChar(50));
        nameColumn.Collation = "Latin1_General_CI_AS";
        nameColumn.Nullable = true;
        Column identityColumn;
        identityColumn = new Column(tbCustomers, "ID", DataType.Int);
        identityColumn.Identity = true;
        identityColumn.IdentitySeed = 1;
        identityColumn.IdentityIncrement = 1;
        Column joinDateColumn;
        joinDateColumn = new Column(tbCustomers,"JoinDate", DataType.DateTime);
        joinDateColumn.Nullable = false;
        Column ExpiryDateColumn;
        ExpiryDateColumn = new Column(tbCustomers, "ExpiryDate", DataType.DateTime);
        ExpiryDateColumn.Nullable = false;
            // disconnect
            srv.DetachDatabase("KarensDatabase", true);
        catch (Exception ex)
            // disconnect failed, delete the database

    If you need to populate some tables this can be done via SqlClient data provider via a connection and command. Using a SQL INSERT statement e.g.

    INSERT INTO SomeTable VALUES (N'Fly Girls' ), (N'Coffee Paradise' ), (N'Garrys Coffee' ), (N'Salem Boat Rentals' ), (N'Knifes are us' )

    Which would insert the records. If you need values with specific identifiers we can use a script such as this.

    SET IDENTITY_INSERT [dbo].[Customer] ON; 
    INSERT  [dbo].[Customer]
            ( [Identifier] ,
    VALUES  ( 1 ,
              N'Fly Girls' ,
              N'Tim Clark' 
    INSERT  [dbo].[Customer]
            ( [Identifier] ,
    VALUES  ( 2 ,
              N'Coffee Paradise' ,
              N'Ann Adams'
    SET IDENTITY_INSERT [dbo].[Customer] OFF;
    See also my code sample for working with the same operations via SMO and SQL.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 8, 2018 1:35 AM
  • Hello Alex,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu

    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

    Sunday, January 28, 2018 12:45 PM