How does the installer, like PetShop, hit the database as it was packaged?
Focus on how to automatically create a database for customers in the installation package
Steps:
1, add a new project-> Select the class Library template-> named DBCustomAction
2, click the Item right key-> Add New Item-> Select Installer class (named DBCustomAction.cs)
3. Add-> in Server Explorer connect to database-> Specify user password (choose Allow to save password)-> database Select Master
4. Switch to DBCustomAction.cs view state-> 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] () 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: can also be exported directly with SQL Server)
6, in the right key properties of Sql.txt-> build operations-> Embedded Resources
7, switch DBCustomAction.cs to Code view, 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 a new project, (select Add to Solution)-> project type-> named DBCustomAction Installer
9, select Application Folder-> add-> project Output- > main output
10, in Solution Explorer-> right-click Installation Project (DBCustomAction Installer)-> view-> user Interface
11, select Startup node-> Add dialog-> text a
12, select the text box a-> right-> up to the top
13, select text Box A property-> modify BannerText, (Specify Database Name)
14, modify BodyText (this dialog allows you to specify the name of the "database to" created on the database server.
15, modify the EditLabel1 (Name of DB), modify Edit1porperty (CUSTOMTEXTA1), and set the other edit2,3,4 edit (2,3,4) Visible property to false;
16, in the Scenario Explorer,-> the right Key installation project (DBCustomAction Installer)-> view-> Custom Action
17, select Installation node-> add-> Double-click the application Folder-> The primary output is from DBCustomAction (active)-> right-key property->customactivedata property modified to/dbname=[customtexta1]
18, compilation build, ok!