Skip to content

How to add database into the while iam deploying.

prabhu raj

Asked Apr 23, 2008, 10:35 PM EDT1 answer
Share
LinkedInX
may i know how to add sqlexpress database to my visual studio project while iam deploying...
(i tried by simply adding database into my project through solution explore before deploy , but while instaling deployed project into client machine ,on that time i cant access the data's from database.)
can you tell me how to do this process in detail.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    you can create it during the installation

    Add an Installer class to your application

    Add a data connection object as follow

    1. In Server Explorer, select Data Connections. Right-click and choose Add Connection.

    2. In the Choose Data Source dialog box, select Microsoft SQL Server.

    3. In the Add Connection dialog box, do the following:

      1. In the Server name drop-down list, type or select a server name.

      2. Select Use Windows Authentication.

      3. In the database box, type master.

      4. Click OK to close the dialog box.

    4. From the Data menu, click Add New Data Source, then use the wizard to add the connection that you established in the previous steps. To verify that the data source is in the project, click Show Data Sources on the Data menu.

    Add a new item TextFile  into your project name it  dbscript.txt for instance
    and put all the necessary script for creating you sql data base
    1. In Solution Explorer, select sql.txt. In the Properties window, set the BuildAction property to Embedded Resource.

    2. Add this code to the Installer class

    using System.IO;
    using System.Collection;
    private
    string GetSql(string Name)
    {  
    try {      

            // Gets the current assembly.
            Assembly Asm = Assembly.GetExecutingAssembly();      
            // Resources are named using a fully qualified name.
            Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);      

            // Reads the contents of the embedded file.
            StreamReader reader = new StreamReader (strm);
            return reader.ReadToEnd();
        }  
        catch (Exception ex) {
            Interaction.MsgBox("In GetSQL: " + ex.Message);
            throw ex
        }
    }
    private
    void ExecuteSql(string DatabaseName, string Sql){
      SqlCommand Command = new SqlCommand(Sql, sqlConnection1);  
        Command.Connection.Open();
        Command.Connection.ChangeDatabase(DatabaseName);
        try {
            Command.ExecuteNonQuery();
        }
        finally {
            // Finally, blocks are a great way to ensure that the connection
            // is always closed.
            Command.Connection.Close();
        }
    }
    protected
    void AddDBTable(string strDBName)
    {
        try {
            // Creates the database.
            ExecuteSql("master", "CREATE DATABASE " + strDBName);      
            // Creates the tables.
            ExecuteSql(strDBName, GetSql("sql.txt"));
        }  
        catch (Exception ex) {
            // Reports any errors and abort.
           Interaction.MsgBox("In exception handler: " + ex.Message);
            throw ex;
        }
    }
    public
    override void Install( System.Collection.IDectionary stateSaver)
    {  
        base.Install(stateSaver);
        AddDBTable(this.Context.Parameters.Item("dbname"));
    }

    • Create a deployment project

    1. On the File menu, click Add, and then click New Project.

    2. In the Add New Project dialog box, open the Other Project Types node and select Setup and Deployment Projects in the Project Type pane. Then select Setup Project in the Templates pane. In the Name box, type DBCustomAction Installer.

    3. In the Properties window, select the ProductName property and type DB Installer.

    4. In the File System Editor, select the Application Folder. On the Action menu, click Add, and then click Project Output.

    5. In the Add Project Output Group dialog box, select Primary output for the DBCustomAction project.

    • Create a custom installation dialog

    1. Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose User Interface.

    2. In the User Interface Editor, select the Start node under Install. On the Action menu, choose Add Dialog.

    3. In the Add Dialog dialog box, select the Textboxes (A) dialog, then click OK.

    4. On the Action menu, choose Move Up. Repeat until the Textboxes (A) dialog is above the Installation Folder node.

    5. In the Properties window, select the BannerText property and type Specify Database Name.

    6. Select the BodyText property and type This dialog allows you to specify the name of the database to be created on the database server.

    7. Select the Edit1Label property and type Name of database:.

    8. Select the Edit1Property property and type CUSTOMTEXTA1.

    9. Select the Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to False.

    • Create a custom action

    1. Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and then click Custom Actions.

    2. In the Custom Actions Editor, select the Install node. On the Action menu, choose Add Custom Action.

    3. In the Select item in project dialog box, double-click the Application Folder.

    4. Select Primary output from DBCustomAction (Active), then click OK to close the dialog box.

    5. Make sure that Primary output from DBCustomAction (Active) item is selected in the Custom Actions Editor. In the Properties window, select the CustomActionData property and type /dbname=[CUSTOMTEXTA1].

    6. On the Build menu, choose Build DBCustomActionInstaller.


    1. In Windows Explorer, navigate to your project directory and find the built installer. The default path will be \documents and settings\yourloginname\DBCustomAction Installer\project configuration\DBCustomAction Installer.msi. The default project configuration is Debug.

    2. Copy the DBCustomAction Installer.msi file and all other files and subdirectories in the directory to another computer.

    That's all

    0