I am working through an msdn example in VS08 on how to create a custom installer and I have followed it to the letter. The only change I made was that I implemented the code in C# instead of VB. It compiles without any errors and appears to install, but the overridden install method is not being called and therefore, nothing is being “installed.”
From what I have read online while researching this problem, I have discovered that some people have had problems getting their uninstall method to be called, but their install method works fine. I decided to follow through on some of those suggestions thinking it might inadvertently help me, so my code is a little more flushed out than the msdn example, but none of the overridden functions at the bottom of my code are being called.
I have paid close attention to detail, but because of the seeming absence of documentation on this problem, I feel like I must be missing something simple. My code for the class library is below.
using System;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration.Install;
namespace DBCustomAction
{
public partial class CsDeployInstaller : Installer
{
public CsDeployInstaller()
{
InitializeComponent();
}
private string GetSql(string Name)
{
try
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream strm = asm.GetManifestResourceStream(asm.GetName().Name + "." + Name);
StreamReader reader = new StreamReader(strm, System.Text.Encoding.Default);
// System.Text.Encoding.ASCII;
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:" + ex.Message);
throw ex;
}
}
private void ExecuteSql(string DataBaseName, string Sql)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["masterConnectionString"].ToString();
//string ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=master;Integrated Security=True";
SqlConnection sqlConnection1 = new SqlConnection(ConnectionString);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, sqlConnection1);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
ExecuteSql("master", "CREATE DATABASE " + strDBName);
ExecuteSql(strDBName, GetSql("sql.txt"));
}
catch (Exception ex)
{
Console.WriteLine("In exception handler:" + ex.Message);
}
}
public override void Install(IDictionary stateSaver)
{
Console.WriteLine("Install is working");
base.Install(stateSaver);
AddDBTable(this.Context.Parameters["dbname"]);
}
public override void Uninstall(IDictionary savedState)
{
Console.WriteLine("I am uninstalling this");
base.Uninstall(savedState);
}
public override void Commit(IDictionary savedState)
{
Console.WriteLine("Commit Function");
base.Commit(savedState);
}
public override void Rollback(IDictionary savedState)
{
Console.WriteLine("rollback works!");
base.Rollback(savedState);
}
}
}
None of the Console.WriteLine commands ever get called. Just for fun, I also ran a trace to detect if the database was even being acknowledged by the installer and there was nothing.
Any help would be greatly appreciated!
I don’t know how else to answer your question other than that this one works:
http://msdn.microsoft.com/en-us/library/9cdb5eda(v=VS.100).aspx
If you follow those steps you should be good.