Key points to introduce how to automatically create databases for customers in the installation package
step:
1, add a new item-> Select Class Bank Template -> Named DBCustomAction
2, click the project right click -> Add new item -> Select the installer class (named dbcustomaction.cs)
3. Add-> Connect to Database -> Specify User Password (Select Allow Save Password) -> Database Select Master
4. Switch to DBCustomAction.cs View Status -> Drag Master.dbo in the Server Explorer Database Connection to Designer
5, add a new item SQL.txt (note to use lowercase), enter the following SQL code
Create Table [DBO]. [Mk_employees]
[Name] [char] (30) Collate SQL_LATIN1_GENER_CP1_CI_AS NULL,
[Rsvp] [int] NULL,
[Requests] [nvarchar] (4000) Collate SQL_LATIN1_GENER_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 export directly with SQLServer)
6. Right click on SQL.TXT -> Generate Operation -> Embedded Resources
7. Switch DBCUSTOMACTION.CS to code view, add the following code
Private string getsql (String name)
{
Try
{
AskEMBLY ASM = askEMBLY.GETEXECUTISSEMBLY ();
STREAM STRM = asm.getmanifestResourceStream (asm.getname (). Name "." Name);
StreamReader Reader = New StreamReader (STRM);
Return Reader.ReadToend ();
}
Catch (Exception EX)
{
Console.Write ("in getsql:" ex. measureage);
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 a new project, (Select Add to Solution) -> Project Type to Installing Project -> Named DBCustomAction Installer
9. Select the application folder -> Add -> Project Output -> Main Output
10. In the program resource manager -> Right click on the installation item (DBCUSTOMACTION Installer) -> View -> User Interface
11. Check the startup node -> Add dialog -> Text a
12. Select a text box A-> Right button -> Up until the top
13. Select text box a properties -> Modify Bannertext, (Specify Database Name)
14. Modify Bodytext (this Dialog Allows you to specify the name of the database to be create on the database server.)
15. Modify EditLabel1 (Name of DB), modify Edit1PorPerty, set other Edit 2, 3, 4 EDIT (2, 3, 4) Visible property to False;
16. In the program resource manager -> Right click on the installation item (DBCustomAction Installer) -> View -> Custom Action
17. Select the installation node -> Add-> Double-click the application folder -> Main output from dbcustomaction -> right click Properties -> CustomactiveData property modified to / dbname = [CustomTexta1]
18, compile generation, OK!
Note: This article mainly comes from MSDN. This article uses VB.NET, I have rewritten it with C #, please refer to the full URL.
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkwalkthroughusingcustomActionTOCREATEDATABASEDURINSTALLATION.ASP
There is no dialog box for customer database services Server-Name and Password, I think you can accept DBNAME parameters, then receive server-name and password should not be difficult.