How do I make my first “Hello, Data Access Application Deployment” setup program?
We will start with the assumption that you have a data access application. Note that we are only providing here the C# example. The VB.Net example is exactly similar. Why we didn’t include the VB.Net example? Because, the intent here is not to explain the code but the procedure of how to create the data accessing application’s deployment project which is independent of the language being used. All the code we will present here has already been presented in the previous FAQs and explained in much detail.
The first step, then, is to add an installer class. The easiest way to do is to right click the project icon in the solution explorer and select ‘Add New Item…’ and in the pop-up window, select Installer class, name it appropriately and select ‘Open’ button to add it. Now right click the newly added installer class and select view code. You will notice that this new class is automatically inherited from the System.Configuration.Install.Installer class
public class Installer1 : System.Configuration.Install.Installer
The only thing you need to do now in this class is to override the Install() method of the base class, write the code you want to execute when the installation setup is executed. We have written the code to display the Data Link Properties dialog box and save the resulted connection string into an xml file, so later the application can use it.
public override void Install(IDictionary stateSaver)
{
base.Install (stateSaver);
string connStr = "";
MSDASC.DataLinks udl = new MSDASC.DataLinksClass();
ADODB.Connection conn = (ADODB.Connection) udl.PromptNew();
if(conn != null)
{
connStr = conn.ConnectionString;
}
// Write connection string to xml file
XmlDocument xmlDoc = new XmlDocument();
XmlNode xn = xmlDoc.CreateNode(XmlNodeType.Element, "ConnectionString", "");
xn.InnerText = connStr;
xmlDoc.AppendChild(xn);
xmlDoc.Save(@"C:\ConnectionString.xml");
}
This is all for the setup, we have added a button to our database deployment application form which displays the connection string in a message box. It retrieves the connection string from the xml file generated by the installation setup program.
private void btnShowConnStr_Click(object sender, System.EventArgs e)
{
string connStr = "";
// Read connection string from the xml file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\ConnectionString.xml");
XmlNode xn = xmlDoc.SelectSingleNode("ConnectionString");
connStr = xn.InnerText;
MessageBox.Show(connStr, "Connection String from XML file");
}
Now the application is ready. We will start building our setup project. For this, add a new ‘Setup and Deployment’ project to the solution by right clicking the solution and selecting ‘Add New Project…’ in the pop-up window, selecting the ‘Setup Project’ from the ‘Setup and Deployment Projects’ section. Once the setup project is added to the solution, right click it in the solution explorer and select View-->File System. Here right click the application folder icon and select Project Output --> Primary Out put of your data access project. This will automatically add the project dependencies. Now right click the ‘Primary Output of your project’ and create its short cut. Copy this shortcut to the Desktop folder and the User Program Menu and rename shortcut appropriately.
http://www.programmersheaven.com/articles/images/faq/image015.gif
Now to ask the setup project run our custom installation code (we have written in the Installer class in the data access application), we need to add the primary output of our project to the custom actions of the setup project. To do this, right click the setup project in the solution explorer, and select View --> Custom Actions. Here right click ‘Install’ and select ‘Add Custom Action’. In the popup window, select the primary output of the database access application from the application folder and click OK.
http://www.programmersheaven.com/articles/images/faq/image016.gif
This is all we need to do! Rebuild the solution and remove any minor bugs (if they do popup). When your setup project will be built, it will generate a Setup.exe file in its debug (or release folder depending on the project configuration). Run this setup.exe to install this application. The setup will ask you the connection string and save it in the xml file.
http://www.programmersheaven.com/articles/images/faq/image017.gif
After installation is complete, run the application using its desktop icon. When you will click the button on the form, it will read the connection string from the xml file and display it in the message box.
http://www.programmersheaven.com/articles/images/faq/image018.gif
The complete source code of this database access deployment application along with setup files can be downloaded by clicking
here.
Back