Project packaging summary implementation function: Install the specified database name and other files according to the user's custom (iewebcontrols) Step: Create a project: visualC # project, select the class library template, if the name is myInstall, add a new item under this project: select the installation program class (Installer1.cs), here I use the default name.
Project packaging summary implementation function: Install the specified database name and other files according to the user's custom (iewebcontrols) Step: Create a project: visual C # project, select the class library template, if the name is myInstall, add a new item under this project: select the installation program class (Installer1.cs), here I use the default name.
Project package Summary
Function: Install the specified database name and other files according to the user's custom settings, as shown in figure(Iewebcontrols)
Steps:
Create a project:Visual C #Project ,Select a class library Template,For exampleMyInstall,Add a new item under this project: select the installer class (Installer1.cs), Here I use the default name. This installation class library will install databases and other files.
InInstaller1.csHere we will rewrite the installation class method: see reference1
InstallCommitRollbackUninstall
InInstallMethod To implement custom installation:
First, we need to get some user variables (user operations during installation, such as database management accounts)
We can use the Context Environment to obtain these variables.This. Context. Parameters ["DataBaseName"];(It will be set in the installation project)
Here we get a total of five variables. The server where the database is installed, the specified database name, Database Administrator account, and password. And database backup files.
Let's take a look at the specific code, which is very simple :)
Private void InstallDateBase ()
{
System. Reflection. AssemblyAsm;
//Asm = System. Reflection. Assembly. GetExecutingAssembly ();
//System. IO. FileInfo = new System. IO. FileInfo (Asm. Location );
//You can also obtain the currently installed directory, as shown in figureWwwrootDirectoryMyweb/Your installation project name. Dll
//The following are database connections and commands
SqlConnection sqlConnection1 = newSqlConnection ();
StringDBName = this. Context. Parameters ["DataBaseName"];
StringDBServer = this. Context. Parameters ["ServerName"];
StringDBUser = this. Context. Parameters ["InStallAdmin"];
StringDBKey = this. Context. Parameters ["InstallPassword"];
StringDir = this. Context. Parameters ["dir"];
StreamWriter ss = newStreamWriter (@ "c:/aaa.txt", true);
Ss. WriteLine ("data source =" + DBName + "; database = master; uid =" + DBUser + "; password =" + DBKey );
Ss. Close ();
StringCONSTR = "data source =" + DBServer + "; database = master; uid =" + DBUser + "; password =" + DBKey;
SqlConnection1.ConnectionString =CONSTR;
//The database will be installedSystem32Lower
StringCurDir = Directory. GetCurrentDirectory () + @ "/TestData/database ";
If(! Directory. Exists (CurDir ))//Create
{
Directory. CreateDirectory (CurDir );
}
StringMSQL = "restore database" + DBName +
"From disk = '" + dir + @ "/database/myDataBase. bak'" +
"With move 'mydate _ dat 'TO'" + CurDir + @ "/" + DBName + ". mdf '," +
"MOVE 'mydate _ log' TO '" + CurDir + @ "/" + DBName + ". ldf '";
SqlCommand cmd = newSqlCommand (MSQL, sqlConnection1 );
Cmd. Connection. Open ();
Cmd. ExecuteNonQuery ();
Cmd. Connection. Close ();
If(SqlConnection1.State = ConnectionState. Open)
{
SqlConnection1.Close ();
}
//InstallTreeView
ProcessStartInfo psi = newProcessStartInfo ();
Psi. WorkingDirectory = dir + "// database ";
Psi. FileName = dir + @ "/database/iewebcontrols. msi ";
Psi. UseShellExecute = true; // MsiFile, suchExeDo not set
Process. Start (psi );
//Which of the following brothers can help you with this. The following code. During execution, a process is running, which causes the control to fail to run during installation. It must be closed. Only.However, the following code is a little better than the above Code. The following process will continue after the end.InstallOther code of the method. The above code, in actual running, the entire installation is complete. And installTreeViewThe job is just getting started. A little out of sync
//System. Diagnostics. Process myproc = new System. Diagnostics. Process ();
//Myproc. EnableRaisingEvents = true;
//
//String file = dir + @ "/database/iewebcontrols. msi ";
//
//Myproc. StartInfo. UseShellExecute = true;
//Myproc. StartInfo. FileName = file;
//Myproc. Start ();
//Myproc. WaitForExit ();//This method can run the following code indefinitely after the process is completed.
//If (myproc. HasExited = true)
//{
////Myproc. Kill ();
//
//}
}
PublicOverrideVoidInstall (System. Collections. IDictionary stateSaver)
{
Try
{
Base. Install (stateSaver );
This. InstallDateBase ();//Call the above method
}
Catch
{
Throw;
}
}
PublicOverrideVoidUninstall (System. Collections. IDictionary stateSaver)
{
Base. Uninstall (stateSaver );
// UnInstallDataBase ();
}
PublicOverrideVoidCommit (System. Collections. IDictionary stateSaver)
{
Base. Commit (stateSaver );
}
PublicOverrideVoidRollback (System. Collections. IDictionary stateSaver)
{
Base. Rollback (stateSaver );
}
Reference1:
Ms-help: // MS. VSCC/MS. MSDNVS.2052/Vbcon/html/vbtskoverridingdefamethomethodsoninstallationcomponent.htm
Ms-help: // MS. VSCC/MS. MSDNVS.2052/vsintro7/html/vxwlk1_throughusingcustomactiontocreatedatabaseduringinstallation.htm
Now let's add a new project under this project.
Install and deploy the project, selectWebProject template, such as nameWebsetup2.
Create a deployment project
- In"File"Choose"Add Project"->"Create a project".
- In"Add Project"In the dialog box, select"Project Type"In the pane"Install and deploy a project"And then select"Template"In the pane"WEBInstallation Project". In"Name"Box, type Websetup2.(I am the default one. I didn't change it. These statements are too lazy to be copied ,:))
- In"Attribute"Window, selectProductNameProperties and type MywebTest.
- In the file system Editor, select"Application folder". In"Operation"Choose"Add"->"Project output".
- In"Add project output Group"In the dialog box"MyInstall"Select primary output for the project.
The previous operations are the same as general packagingWEBAdd some files in the project. And some other packaging settings (product name, etc.
We only need to add a database backup file (MyDataBase. bak)
OpenWebsetup2File System window, selectWebApplication folder. Add a folder under this folder to store the database, as shown in figureDatabase,AddMydatabase. bakCopy the file to this directory.
Now we need to add a installation window for users to enter database accounts. You only need to open the user interface window to perform operations.
Create custom installation dialog box
- In Solution Explorer, select"Websetup2"Project. In"View"Point"Editor"And then select"User Interface".
- In the User Interface Editor, select"Install"Under"Start"Node. In"Operation"Choose"Add dialog box".
- In"Add dialog box"Select"Text Box()"Dialog box.
- In"Operation"Select"Move up". Repeat this step"Text Box()"The dialog box is located in"Installation Folder"Node.
- In"Attribute"Window, selectBannerTextProperties and typeCustom Database Installation
- Select BodyTextProperties and typeCustom Database Installation
- SelectEdit1LabelProperties and typeServer Name:.
- SelectEdit1PropertyProperties and type SERVERNAME.
- SelectEdit2LabelAttribute and type the name of the database to be created:.
- SelectEdit2PropertyProperties and type DATABASENAME.
- SelectEdit3LabelProperties and typeDatabase Administrator Account:.
- SelectEdit3PropertyProperties and type INSTALLADMIN.
- SelectEdit4LabelProperties and typeAdministrator Password:.INSTALLPASSWORD
- SelectEdit4PropertyProperties and type SERVERNAME.
Create a custom operation
- In Solution Explorer, select"Websetup2"Project. In"View"Point"Editor"And then select"Custom operations".
- Select"Install"Node. In"Operation"Choose"Add custom operations".
- In"Select items in the project"In the dialog box, double-click"Application folder".
- Select"Main output fromDBCustomAction(Activity)".
In"Attribute"Window, selectCustomActionDataProperties and type /InstallPassword = [INSTALLPASSWORD]/InStallAdmin = [INSTALLADMIN]/ServerName = [SERVERNAME]/DataBaseName = [DATABASENAME]/dir = "[TARGETDIR]/"
NoteThere is a space between user data. For example/Aaa = [fdfad]/bfff = [fdafdfd]
- In"Generate"Select"GenerateWebsetup2".
The aboveCustomActionDataWill be referenced in the installation class. Note that there must be spaces between each other.
/Dir = ["TARGETDIR"],The installation is final.WEBDirectory
The package of this project has ended.