Click here to Skip to main content
15,884,998 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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,
C#
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
Posted
Updated 16-May-12 21:13pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900