Http://herald.seu.edu.cn/blog/snow/articles/44990.aspx
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.ProgramClass (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 reader = new streamreader (STRM );
Return reader. readtoend ();
}
Catch (exception ex)
{
Console. Write ("in getsql:" + ex. Message );
Throw ex;
}
}
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
{
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 Editlabel1 (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!
Note:ArticleIt mainly comes from msdn. The original code uses VB.net and I have rewritten it with C #. For the complete URL, see
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/vsintro7/html/vxwlk?throughusingcustomactiontocreatedatabaseduringinstallation. asp;
In addition, the dialog box does not accept 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.