Package and release (Automatic Installation of SQL database)

Source: Internet
Author: User

(1). Create a deployment project

1. Point to "add project" on the "file" menu and select "new project ".

2. In the "Add new project" dialog box, select "Install and deploy Project" in the "project type" pane, and select "Install Project" in the "template" pane ". In the Name box, type setup1.

3. Click OK to close the dialog box.

4. The project is added to Solution Explorer and opened in the file system editor.

5. In the "properties" window, select productname properties and type the 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 "operations" menu, point to "add" and select "project output ".

2. In the "add project output group" dialog box, select "your program" from the "project" drop-down list ".

3. Click OK to close the dialog box.

4. Select the "main output" and "Content File" groups from the list, and click "OK ".

3). Create the installer class

1. Point to "new" on the "file" menu and select "project ".

2. In the "new project" dialog box, select "Visual Basic Project" in the "project type" pane and select "class library" in the "template" pane ". In the "name" box, type installdb.

3. Click Open to close the dialog box.

4. Select "Add new project" from the "project" menu ".

5. In the "Add new project" dialog box, select "Installer class ". In the "name" box, type installdb.

6. Click OK to close the dialog box.

7. The 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 select "User Interface ".
2. In the User Interface Editor, select the "Start" node under "installation. On the "operations" menu, select "add dialog box ".
3. In the "add dialog box", select the "License Agreement" dialog box and click "OK" to close the dialog box.
4. In the Add dialog box, select the text box (A) and click OK to close the dialog box.
5. On the "operations" menu, select "Move Up ". Repeat this step until the "text box (a)" dialog box is located on the "Install Folder" node.
6. In the "properties" window, select the bannertext attribute and type: Install database.
7. Select the bodytext attribute and type: the installer will install the database on the target machine.
8. Select the edit1label attribute and type: Database Name:
9. Select the edit1property property and type customtexta1
10. Select the edit1value attribute and type dbservers.
11. Select the edit2label attribute and type: Server Name:
12. Select the edit2property property and type customtexta2
13. Select the edit2value attribute and type: (local)
14. Select the edit3label attribute and type: User Name:
15. Select the edit3value attribute and type: SA
16. Select the edit3property property and type customtexta3
17. Select the edit4label attribute and type: password:
18. Select the edit4property property and type customtexta4
19. Select the edit2visible, edit3visible, and edit4visible attributes and set them to true.

5). Create a custom operation

1. Select the "setup1" project in Solution Explorer. On the "View" menu, point to "Editor" and select "Custom operations ".
2. Select the "Install" node in the Custom operation editor. On the "operations" menu, select "add custom operation ".
3. In the "select project items" dialog box, double-click "application folder ".
4. Select "primary output from installdb (activity)" and click "OK" to close the dialog box.
5. in the "properties" window, select the customactiondata attribute and type "/dbname = [customtexta1]/Server = [customtexta2]/user = [customtexta3]/Pwd = [customtexta4]/targetdir =" [targetdir]/"".

Appendix:/targetdir = "[targetdir]/" is the target path after installation. To obtain the path after installation in the installdb class, we set this parameter.

6). added the uninstall function during packaging:

Method 1:

1. Upload the msiexec.exe file in the package object (generally found in C:/Windows/system32)

2. Upload the program file in the file system, press "right" on "msiexec.exe", select "Uninstall" to create a shortcut, and rename the shortcut to "Uninstall ".

3. Change the arguments of this shortcut to "/X {product ID}", and the value of product ID is the productcode validation value of the packaging destination.

Method 2: (recommended)

1. Complete the installation package, and write down the productcode (select the solution Resource Manager root directory such as setup1, and then view the property tag, not the right-click attribute ).

2.use vs.netto 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}") "changed to your own productcode

End if

Myprocess. Close ()

End sub

End Module

3.add the exefiles in the binlog of the console program to the package program file, and create the uninst.exe shortcut in the program group.

Installdb. VB class, to add reference 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 of installer1.

/// </Summary>

[Runinstaller (true)]

Public class installer1: system. configuration. Install. Installer

{

/// <Summary>

/// Required designer variables.

/// </Summary>

Private system. componentmodel. Container components = NULL;
Public installer1 ()

{

// This call is required by the designer.

Initializecomponent ();
// Todo: add any initialization after initializecomponent calls

}
/// <Summary>

/// Clear all resources in use.

/// </Summary>

Protected override void dispose (bool disposing)

{

If (disposing)

{

If (components! = NULL)

{

Components. Dispose ();

}

}

Base. Dispose (disposing );

}

# Code generated by the region component designer

/// <Summary>

/// The designer supports the required methods-do not use the code editor to modify

/// 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 (); // wait for execution

//

// Sqlprocess. Close ();

Try

{

// Assembly ASM = assembly. getexecutingassembly ();

// System. Io. 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 'myamaster', 'myamaster', '" + strdbname + "', null, null ");

Executesql (strdbname, "Exec sp_grantdbaccess 'myamaster', 'myamaster '");

Executesql (strdbname, "Exec sp_addrolemember 'db _ owner', 'myamaster '");

}

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"]);

}

}

}
Here, SQL .txt is the SQL script of the database. Of course, you can call osql to execute the SQL script, which is actually the same.
When the package is completed, add the SQL .txt file. Otherwise, it will not be executed.
If you want to append the MDF and LDF files 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 '// 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 should also be added during packaging.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.