Wednesday, 8 August 2012
DotNet Solutions: Custom Installation_Changing Connection String dur...
DotNet Solutions: Custom Installation_Changing Connection String dur...: Step1: Open an Console application add installer class,App.config file Step 2: Add new project other projects type setup and Deployment ch...
Friday, 3 August 2012
Custom Installation_Changing Connection String during installation
Step1: Open an Console application add installer class,App.config file
Step 2: Add new project other projects type setup and Deployment chosse Setup Class.
Step3: istaller class code:
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
public Installer1()
{
InitializeComponent();
}
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
}
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
//string str = ConfigurationSettings.AppSettings["SqlConnectionString"];
//foreach (string key in Context.Parameters.Keys)
//{
// MessageBox.Show(Context.Parameters[key].ToString());
//}
string dataSource = Context.Parameters["DataSource"];
dataSource = dataSource.Replace('/', ';');
MessageBox.Show("instance=" + dataSource);
dataSource = dataSource.Replace("$", @"\");
dataSource = dataSource.Replace("UserID", @"User ID");
dataSource = "Data source = " + dataSource.Replace("InitialCatalog", @"Initial Catalog");
MessageBox.Show("instance=" + dataSource);
//MessageBox.Show(userName);
string pwd = Context.Parameters["Password"];
//MessageBox.Show(pwd);
//string connectionString = "DataSource=" + dataSource ;
//MessageBox.Show("Connection string = " + connectionString);
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
//map.ExeConfigFilename = @"C:\Program Files\Cit\CustomFormInstalSetup\";
MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
//Getting the path location
string configFile = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
map.ExeConfigFilename = configFile;
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
string connectionsection = config.ConnectionStrings.ConnectionStrings["SqlConnectionString"].ConnectionString;
MessageBox.Show("connectionsection" + connectionsection);
ConnectionStringSettings connectionstring = null;
if (connectionsection != null)
{
config.ConnectionStrings.ConnectionStrings.Remove("SqlConnectionString");
MessageBox.Show("removeing existing Connection String");
}
MessageBox.Show(" add new Connection");
//connectionstring = new ConnectionStringSettings("SqlConnectionString", connectionString);
connectionstring = new ConnectionStringSettings("SqlConnectionString", dataSource);
config.ConnectionStrings.ConnectionStrings.Add(connectionstring);
MessageBox.Show("Added " + connectionstring.ToString());
config.Save(ConfigurationSaveMode.Modified,true);
ConfigurationManager.RefreshSection("connectionStrings");
SqlConnection con = new SqlConnection(connectionstring.ConnectionString);
MessageBox.Show(" openning to Dbconnection");
try
{
con.Open();
MessageBox.Show(" connection opened ");
}
catch (Exception e)
{
}
finally
{
con.Close();
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
// Code maybe written for installation of an application.
Console.WriteLine("The Install method of 'MyInstallerSample' has been called");
}
//public override void Install(System.Collections.IDictionary stateSaver)
//{
// base.Install(stateSaver);
// stateSaver.Add("cbvalue", Context.Parameters["cbvalue"].ToString());
//}
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
}
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
}
Step4: Setup project Right click Vews then User interface. right click on start add diloug and add text bok
Step 5: Add custom action data <Key> <Value>. Setup project Right click Custom actions
/DataSource=[EDITA1]/InitialCatalog=[EDITA2]/UserID=[EDITA3]/Password=[EDITA4]
Text box label name should match to the key value and property name mathed to Value.
We should provide custom action data to primary out put value on install only.
Step6: rigt click and build the project and setup file was created in debug folder install it and check the config file in installed location C:programing files /path name ...... you will get new connection string with details provided in the text boxes
Step 2: Add new project other projects type setup and Deployment chosse Setup Class.
Step3: istaller class code:
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
public Installer1()
{
InitializeComponent();
}
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
}
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
//string str = ConfigurationSettings.AppSettings["SqlConnectionString"];
//foreach (string key in Context.Parameters.Keys)
//{
// MessageBox.Show(Context.Parameters[key].ToString());
//}
string dataSource = Context.Parameters["DataSource"];
dataSource = dataSource.Replace('/', ';');
MessageBox.Show("instance=" + dataSource);
dataSource = dataSource.Replace("$", @"\");
dataSource = dataSource.Replace("UserID", @"User ID");
dataSource = "Data source = " + dataSource.Replace("InitialCatalog", @"Initial Catalog");
MessageBox.Show("instance=" + dataSource);
//MessageBox.Show(userName);
string pwd = Context.Parameters["Password"];
//MessageBox.Show(pwd);
//string connectionString = "DataSource=" + dataSource ;
//MessageBox.Show("Connection string = " + connectionString);
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
//map.ExeConfigFilename = @"C:\Program Files\Cit\CustomFormInstalSetup\";
MessageBox.Show(Assembly.GetExecutingAssembly().Location + ".config");
//Getting the path location
string configFile = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
map.ExeConfigFilename = configFile;
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
string connectionsection = config.ConnectionStrings.ConnectionStrings["SqlConnectionString"].ConnectionString;
MessageBox.Show("connectionsection" + connectionsection);
ConnectionStringSettings connectionstring = null;
if (connectionsection != null)
{
config.ConnectionStrings.ConnectionStrings.Remove("SqlConnectionString");
MessageBox.Show("removeing existing Connection String");
}
MessageBox.Show(" add new Connection");
//connectionstring = new ConnectionStringSettings("SqlConnectionString", connectionString);
connectionstring = new ConnectionStringSettings("SqlConnectionString", dataSource);
config.ConnectionStrings.ConnectionStrings.Add(connectionstring);
MessageBox.Show("Added " + connectionstring.ToString());
config.Save(ConfigurationSaveMode.Modified,true);
ConfigurationManager.RefreshSection("connectionStrings");
SqlConnection con = new SqlConnection(connectionstring.ConnectionString);
MessageBox.Show(" openning to Dbconnection");
try
{
con.Open();
MessageBox.Show(" connection opened ");
}
catch (Exception e)
{
}
finally
{
con.Close();
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
// Code maybe written for installation of an application.
Console.WriteLine("The Install method of 'MyInstallerSample' has been called");
}
//public override void Install(System.Collections.IDictionary stateSaver)
//{
// base.Install(stateSaver);
// stateSaver.Add("cbvalue", Context.Parameters["cbvalue"].ToString());
//}
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
}
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
}
Step4: Setup project Right click Vews then User interface. right click on start add diloug and add text bok
Step 5: Add custom action data <Key> <Value>. Setup project Right click Custom actions
/DataSource=[EDITA1]/InitialCatalog=[EDITA2]/UserID=[EDITA3]/Password=[EDITA4]
Text box label name should match to the key value and property name mathed to Value.
We should provide custom action data to primary out put value on install only.
Step6: rigt click and build the project and setup file was created in debug folder install it and check the config file in installed location C:programing files /path name ...... you will get new connection string with details provided in the text boxes
Subscribe to:
Comments (Atom)
