1).Create a deployment project
1. On the File menu, point to Add Project, and then select New Project.
2. In the Add New Project dialog box, select Installation and Deployment Projects in the Project Types pane, and then select Installation Projects in the Templates pane. Type setup1 in the Name box.
3. Click OK to close the dialog box.
4. The project is added to Solution Explorer and the File System Editor opens.
5. In the Properties window, select the ProductName property and type Information Management System.
2). Add the output of the main program project to the deployment project
1. In the File System Editor, select Application Folder. On the Actions menu, point to Add, and then select Project Output.
2. In the Add Project Output Group dialog box, select Your Program in the Project drop-down list.
3. Click OK to close the dialog box.
4. Select the Main Output and Content Files groups from the list, and click OK.
3).Create the installer class
1. On the File menu, point to New, and then select Project.
2. In the New Project dialog box, select Visual Basic Project in the Project Types pane, and then select Class Library in the Templates pane. Type installDB in the Name box.
3. Click "Open" to close the dialog box.
4. Select Add New Item from the Project menu.
5. Select Installer Class in the Add New Item dialog box. Type installDB in the Name box.
6. Click OK to close the dialog box.
7. Detailed code is attached.
4).Create a custom installation dialog box
1. Select the "setup1" project in Solution Explorer. On the View menu, point to Editor, and then select User Interface.
2. In the User Interface Editor, select the Startup node under Installation. On the Action menu, select Add Dialog.
3. In the Add Dialog dialog box, select the License Agreement dialog box, and then click OK to close the dialog box.
4. In the Add Dialog dialog box, select the Text Box (A) dialog box, and then click OK to close the dialog box.
5. On the Action menu, select Move Up. Repeat this step until the Text Box (A) dialog box is above the Installation Folder node.
6. In the Properties window, select the BannerText property and type: install database.
7. Select the BodyText property and type: The installer will install the database on the target machine
8. Select the Edit1Label property and type: database name:
9. Select the Edit1Property property and type CUSTOMTEXTA1
10. Select the Edit1Value property and type: dbservers
11. Select the Edit2Label property and type: server name:
12. Select the Edit2Property property and type CUSTOMTEXTA2
13. Select the Edit2Value property and type: (local)
14. Select the Edit3Label property and type: username:
15. Select the Edit3Value property and type: sa
16. Select the Edit3Property property and type CUSTOMTEXTA3
17. Select the Edit4Label property and type: Password:
18. Select the Edit4Property property and type CUSTOMTEXTA4
19. Select the Edit2Visible, Edit3Visible and Edit4Visible properties and set them to true
5).Create custom operations
1. Select the "setup1" project in Solution Explorer. On the View menu, point to Editor, and then select Custom Actions.
2. Select the Install node in the custom action editor. On the Actions menu, select Add Custom Action.
3. In the Select Items in Project dialog box, double-click Application Folder.
4. Select the "Main output from installDB (active)" item and click "OK" to close the dialog box.
5. In the Properties window, select the CustomActionData property and type "/dbname=[CUSTOMTEXTA1] /server=[CUSTOMTEXTA2] /user=[CUSTOMTEXTA3] /pwd=[CUSTOMTEXTA4] /targetdir="[TARGETDIR]"".
Attachment: /targetdir="[TARGETDIR]" is the target path after installation. In order to obtain the path after installation in the installDB class, we set this parameter.
6). Add uninstall function when packaging:
Method one:
1. Add the file msiexec.exe to the packaged project (generally found under c:windowssystem32)
2. Select the application folder in the file system view, right-click on msiexec.exe, select Create shortcut, and rename the shortcut to "Uninstall".
3. Change the Arguments of this shortcut to "/x {product id}", and the value of the product id is the ProductCode attribute value of the packaged project.
Method 2: (recommended)
1. First generate the installation package and write down the ProductCode (select the root directory of the solution explorer such as setup1, and then check the properties label, not the properties in the right-click), which will be used below
2. Use VS.net to create a new console program uninst.exe file
'power by: landlordh
'for 2000,xp,2003
Module uninstall
Sub Main()
Dim myProcess As Process = New Process
If System.Environment.OSVersion.ToString.IndexOf("NT 5") Then
myProcess.Start("msiexec", "/X{2B65D4A9-C146-4808-AB4B-321FB0779559}") 'Change to your own ProductCode
End If
myProcess.Close()
End Sub
End Module
3. Add the exe file in the BIN directory of the console program to the packaged program file, and create a shortcut to uninst.exe in the program group.
installdb.vb class, to add reference to system.configuration.install.dll:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Reflection;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace install
{
/// <summary>
/// Summary description for Installer1.
/// </summary>
[RunInstaller(true)]
public class Installer1 : System.Configuration.Install.Installer
{
/// <summary>
/// Required designer variables.
/// </summary>
private System.ComponentModel.Container components = null;
publicInstaller1()
{
// This call is required by the designer.
InitializeComponent();
// TODO: Add any initialization after InitializeComponent call
}
/// <summary>
/// Clean up all resources in use.
/// </summary>
protected override void Dispose( bool disposing)
{
if(disposing)
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Code generated by the component designer
/// <summary>
/// Designer supports required methods - do not use code editor to modify
/// The content of this method.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
private string GetSql(string Name)
{
// //Call osql to execute the script
//
// System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
//
// sqlProcess.StartInfo.FileName = "osql.exe";
//
// sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql", this.Context.Parameters["user"], this .Context.Parameters["pwd"],"master", this.Context.Parameters["targetdir"]);
//
// sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//
// sqlProcess.Start();
//
// sqlProcess.WaitForExit() ;//Waiting for execution
//
// sqlProcess.Close();
try
{
// Assembly Asm = Assembly.GetExecutingAssembly();
// System.IO.FileInfo FileInfo = new System.IO.FileInfo(Asm.Location);
// string path=FileInfo.DirectoryName+@""+Name ;
string path=this.Context.Parameters["targetdir"]+Name;
FileStream fs=new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read);
StreamReader reader = new StreamReader(fs,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)
{
SqlConnection sqlConnection1=new SqlConnection();
sqlConnection1.ConnectionString =string.Format("server={0}; user id={1}; password={2}; Database=master",this.Context.Parameters["server"],this.Context.Parameters[ "user"],this.Context.Parameters["pwd"]);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
try
{
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
Command.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
finally
{
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
ExecuteSql("master","CREATE DATABASE "+ strDBName);
ExecuteSql(strDBName,GetSql("sql.txt"));
ExecuteSql("master","exec sp_addlogin 'myoamaster','myoamaster','"+strDBName+"',Null,Null");
ExecuteSql(strDBName,"EXEC sp_grantdbaccess 'myoamaster', 'myoamaster'");
ExecuteSql(strDBName,"exec sp_addrolemember 'db_owner','myoamaster'");
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable(this.Context.Parameters["dbname"]);
}
}
}
There is sql.txt here which is the sql script of the database. Of course, you can call osql to execute the sql script. In fact, it is the same.
The sql.txt file must be added when packaging, otherwise it will not be executed.
If you want to attach the mdf file and ldf file of the database, use the following program:
private void CreateDataBase(string strSql,string DataName,string strMdf,string strLdf)
{
String str;
SqlConnection myConn = new SqlConnection (strSql);
//EXEC sp_detach_db @dbname = 'BX_FreightMileage_2'//You need to separate the database first
str = "EXEC sp_attach_db @dbname = '"+ DataName +"', @filename1 = '"+ strMdf +"',@filename2='"+strLdf+"'";
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
}
Of course, these two database files must also be added when packaging.