Use vs.net to create an installer

Source: Internet
Author: User
Focuses on how to automatically create a database for the customer in the installation package

Steps:
1. Add a new project, select a class library template, and name it dbcustomaction.
2. Right-click the project and choose add new item from the shortcut menu. Then, select install. Program Class (named dbcustomaction. CS)
3. In the server resource manager, add-> connect to the database-> specify the user password (select allow to save the password)-> select master for the database
4. Switch to the view status of dbcustomaction. CS-> drag master. DBO from the database connection of the server resource manager to the designer.
A new SQL .txt (in lower case) is entered in the following SQL statement: Code Create   Table   [ DBO ] . [ Mk_employees ] (
[ Name ]   [ Char ] ( 30 ) Collate SQL _latin1_general_cp1_ci_as Not   Null ,
[ RSVP ]   [ Int ]   Null ,
[ Requests ]   [ Nvarchar ] ( 4000 ) Collate SQL _latin1_general_cp1_ci_as Null
) On   [ Primary ] ;

Alter   Table   [ DBO ] . [ Mk_employees ]   With   Nocheck   Add
Constraint   [ Pk_mk_employees ]   Primary   Key   Clustered
(
[ Name ]  
) On   [ Primary ] ;

(P.s: You can also use sqlserver to export data directly)

6 bytes right-click SQL .txt and choose "generate operation"> "embedded resource" from the context menu.
7. Switch dbcustomaction. CS to the Code view and add the following code:

Private String Getsql ( String Name)
{
Try  
{
Assembly ASM = Assembly. getexecutingassembly ();
Stream STRM = ASM. getmanifestresourcestream (ASM. getname (). Name +   " . " + Name );
Streamreader =   New Streamreader (STRM );
Return Reader. readtoend ();
}  
Catch (Exception ex)
{
Console. Write ("In getsql:"+Ex. Message );
ThrowEx;
}  
}  

Private   Void Executesql ( String Databasename, String SQL)
{
System. Data. sqlclient. sqlcommand command =   New System. Data. sqlclient. sqlcommand (SQL, sqlconnection1 );

Command. Connection. open ();
Command. Connection. changedatabase (databasename );
Try  
{
Command. executenonquery ();
}  
Finally  
{
Command. Connection. Close ();
}  
}  


Protected   Void Adddbtable ( String Strdbname)
{
Try  
{< br> executesql ( " master " , " create database " + strdbname );
executesql (strdbname, getsql ( " SQL .txt " ));
}  
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"]);
}  

8. Add another project (select Add to solution) -> the project type is installation project-> name it dbcustomaction installer
9. Select the application folder-> Add-> project output-> main output
10. In Solution resource Manager-> right-click to install the project (dbcustomaction installer) -> View-> User Interface
11. Select Start Node-> Add dialog box-> text a
12. Select the dynamic text box a-> right-click and choose move up top
13. Select "A" in the text box and modify bannertext, (specify Database Name)
14. Modify bodytext (this dialog allows you to specify the name of the database to be created on the database server .)
15. Modify editlabe L1 (name of DB), modify edit1porperty (customtexta1), set other edit2, 3,4 edit (, 4) Visible attributes to false;
16. In Solution Explorer, right-click to install the project (dbcustomaction installer) -> View-> Custom operation
17. Select the installation node-> Add-> double-click the application folder-> main output from dbcustomaction (activity) -> right-click the attribute and choose customactivedata to change it to/dbname = [customtexta1]
18. compile and generate the template. OK!

complete URL See
http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/vsintro7/html/vxwlk1_throughusingcustomactiontocreatedatabaseduringinstallation. ASP
In addition, there is no dialog box for accepting the client database service server-name and password. Since the dbname parameter is acceptable, it is not difficult to receive the server-name and password.

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.