Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can create Setup file with SQL Server database in windows application

Installer Coding

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;

using System.Data.SqlClient;
using System.IO;
using System.Security.AccessControl;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Reflection;
using System.Windows;

namespace MLMSoftware
{
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
SqlConnection masterConnection = new SqlConnection();
public Installer1():base()
{
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)
{
MessageBox.Show("In GetSQL: " + ex.Message);
throw ex;
}
}

private void ExecuteSql(string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, masterConnection);

// Initialize the connection, open it, and set it to the "master" database
masterConnection.ConnectionString = Properties.Settings.Default.masterConnectionString;
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
// Closing the connection should be done in a Finally block
Command.Connection.Close();
}
}

protected void AddDBTable(string strDBName)
{
try
{
// Creates the database.
ExecuteSql("master", "CREATE DATABASE " + strDBName);

// Creates the tables.
ExecuteSql(strDBName, GetSql("tables.txt"));

// Creates the stored procedure.
ExecuteSql(strDBName, GetSql("InsertData.txt"));

}
catch (Exception ex)
{
// Reports any errors and abort.
MessageBox.Show("In exception handler: " + ex.Message);
throw ex;
}
}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable("MLM");
}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
ExecuteSql("master", "DROP DATABASE MLM");
}
}
}

error given
Posted
Updated 25-Sep-14 20:10pm
v3
Comments
Subramanyam Shankar 7-Mar-15 11:09am    
Can you provide exception details please

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