none
creating database using installer class when installing my application RRS feed

  • Question

  • I want to create database and tables on the client system when i install the setup on it.For that i created a installation class and text file for the database creation.In the installer class am calling the sql.txt for creating database.The installer class requires 3 .dll files like,sqlserver.connectioninfo,sqlserver.smo and sqlserver.sdk.sfk like that .I gave these references thrgh browsing option of add reference in the visualstudio.These .dll i took from c/prgmfiles/sqlserver/mssql/sdk/assemblies........but on build it shows warnings like

    Warning 3 Unable to find dependency 'MICROSOFT.SQLSERVER.SERVICEBROKERENUM' (Signature='89845DCD8080CC91' Version='11.0.0.0') of assembly 'Microsoft.SqlServer.Smo.dll' D:\newser\newset\newset.vdproj newset

    Warning 1 Unable to find dependency 'MICROSOFT.SQLSERVER.SQLCLRPROVIDER' (Signature='89845DCD8080CC91' Version='11.0.0.0') of assembly 'Microsoft.SqlServer.Management.Sdk.Sfc.dll' D:\newser\newset\newset.vdproj newset

    Warning 2 Unable to find dependency 'MICROSOFT.SQLSERVER.MANAGEMENT.SQLPARSER' (Signature='89845DCD8080CC91' Version='11.0.0.0') of assembly 'Microsoft.SqlServer.Smo.dll' D:\newser\newset\newset.vdproj newset

    am adding my installer class code here,

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Linq;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Common;
    using System.Reflection;
    using System.IO;
    using System.Data.SqlClient;


    namespace newser
    {
        [RunInstaller(true)]
        public partial class DBInstaller : Installer
        {
            System.Data.SqlClient.SqlConnection masterconnection = new System.Data.SqlClient.SqlConnection();


            private string logFilePath = "\\SetupLog.txt";
            public DBInstaller():base()
            {

                //This call is required by the Component Designer.
                //Add initialization code after the call to InitializeComponent
               
                InitializeComponent();
            }
            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)
                {
                    Log(ex.ToString());
                    throw ex;
                }
            }
            private void ExecuteSql(string serverName, string dbName, string Sql)
            {
                string connStr = "Data Source=localhost;Initial Catalog=master ;Integrated Security=True";
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    try
                    {
                        Server server = new Server(new ServerConnection(conn));
                        server.ConnectionContext.ExecuteNonQuery(Sql);
                    }
                    catch (Exception ex)
                    {
                        Log(ex.ToString());
                    }
                }
            }
            protected void AddDBTable(string serverName)
            {
                try
                {
                    // Creates the database and installs the tables.
                    string strScript = GetSql("sql.txt");
                    ExecuteSql(serverName, "master", strScript);
                }
                catch (Exception ex)
                {
                    //Reports any errors and abort.
                    Log(ex.ToString());
                    throw ex;
                }
            }

            public override void Install(System.Collections.IDictionary stateSaver)
            {
                base.Install(stateSaver);
                Log("Setup started");
                AddDBTable(this.Context.Parameters["local"]);

            }
            public void Log(string str)
            {
                StreamWriter Tex;
                try
                {
                    Tex = File.AppendText(this.logFilePath);
                    Tex.WriteLine(DateTime.Now.ToString() + " " + str);
                    Tex.Close();
                }
                catch
                { }
            }
            public override void Uninstall(IDictionary savedState)
            {
                base.Uninstall(savedState);
            }
            public override void Commit(IDictionary savedState)
            {
                base.Commit(savedState);
            }
            public override void Rollback(IDictionary savedState)
            {
                base.Rollback(savedState);
            }

           
        }
    }

    here database i set it directly,ie not thrgh customising..........i can change it.........

    anyone please give the proper instruction...stucked due to this and am fresher...........one more thing my sql.txt contains only create tables......

    plz answer me that wethr there is any problem in my installer class and reference also without using installer class how can i do my database creation?


    rahul

    Thursday, May 17, 2012 4:58 AM

Answers

  • Hi Rahul,

    Welcome to the MSDN Forum.

    First of all, I haven't found any obvious errors in your above code.

    In you code, I don't see you used the reference of such "MICROSOFT.SQLSERVER.MANAGEMENT.SQLPARSER", so please try to remove unused references from your project and try to rebuild it again.

    And another thing, these warning is from "newset.vdproj" project, is your above code in this project? 

    to remove this warning, this thread should be helpful: http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/3e7a2800-2547-40bb-b7bd-cca6bc71aea0 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 18, 2012 9:29 AM
    Moderator

All replies

  • Hi Rahul,

    Welcome to the MSDN Forum.

    First of all, I haven't found any obvious errors in your above code.

    In you code, I don't see you used the reference of such "MICROSOFT.SQLSERVER.MANAGEMENT.SQLPARSER", so please try to remove unused references from your project and try to rebuild it again.

    And another thing, these warning is from "newset.vdproj" project, is your above code in this project? 

    to remove this warning, this thread should be helpful: http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/3e7a2800-2547-40bb-b7bd-cca6bc71aea0 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 18, 2012 9:29 AM
    Moderator
  • Dear dudes,

    I want to user Installer class to create SQL Server Database in Visual Studio 2012. What should I do? Is there any other solution to create SQL Server Database automatically when publish windows application?

    Friday, July 12, 2013 7:03 PM