See ASP. NET and SQL together for packaging, deployment, and installation.ArticleIt is packaged for VB. NET and SQL, but I use C #. Of course, you only need to modify the main installation class library! C # class libraryCodeDbcustomaction. CS
Using system;
Using system. collections;
Using system. Data. sqlclient;
Using system. componentmodel;
Using system. configuration. Install;
Using system. diagnostics;
Using system. IO;
Using system. xml;
Using system. reflection;
Namespace PMS
{
/// <Summary>
/// Summary of dbcustomaction.
/// </Summary>
[Runinstaller (true)]
Public class dbcustomaction: system. configuration. Install. Installer
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Public dbcustomaction ()
{
// This call is required by the designer.
Initializecomponent ();
// Todo: add any initialization after initializecomponent calls
}
private void executesql (string Conn, string databasename, string SQL)
{< br> sqlconnection mysqlconnection = new sqlconnection (conn );
sqlcommand command = new sqlcommand (SQL, mysqlconnection);
mysqlconnection. open ();
mysqlconnection. changedatabase (databasename);
try
{< br> command. executenonquery ();
}< br> finally
{< br> // close connection
mysqlconnection. close ();
}< BR >}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
//
Public override void install (system. Collections. idictionary statesaver)
{
Base. Install (statesaver );
// ------------------------ Create a database -------------------------------------------------
Try
{
String connstr = string. format ("Data Source = {0}; user id = {1}; Password = {2}; persist Security info = false; packet size = 4096", context. parameters ["server"], context. parameters ["user"], context. parameters ["PWD"]);
// 'Create a database based on the input database name
Executesql (connstr, "Master", "create database" + context. Parameters ["dbname"]);
// 'Call osql to execute the script
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 ", context. parameters ["user"], context. parameters ["PWD"], context. parameters ["dbname"], context. parameters ["targetdir"]);
Sqlprocess. startinfo. windowstyle = processwindowstyle. hidden;
Sqlprocess. Start ();
Sqlprocess. waitforexit (); // 'waiting for execution
Sqlprocess. Close ();
// 'Delete the script file
Fileinfo sqlfileinfo = new fileinfo (string. Format ("{0} dB. SQL", context. Parameters ["targetdir"]);
If (sqlfileinfo. exists)
{
Sqlfileinfo. Delete ();
}
}
Catch (exception ex)
{
Throw ex;
}
// '--------------------- Write the connection string to the Web. config -----------------------------------
/*
Try
{
Fileinfo = new fileinfo (context. Parameters ["targetdir"] + "http://www.cnblogs.com/yuanlei347/admin/file://web.config ");
If (! Fileinfo. exists)
{
Throw new installexception ("No configuration file found ");
}
// 'Instantiate the XML document
Xmldocument = new xmldocument ();
Xmldocument. Load (fileinfo. fullname );
// 'Find the node in the ettings.
// Xmlnode node = new xmlnode ();
Boolean foundit = false;
Foreach (xmlnode node in xmldocument. selectsinglenode ("deletenodes"). childnodes)
{
If (node. Name = "add ")
{
If (node. Attributes. getnameditem ("key"). value = "connstring ")
{
// 'Write the connection string
Node. attributes. getnameditem ("value "). value = string. format ("Persist Security info = false; Data Source = {0}; initial catalog = {1}; user id = {2}; Password = {3 }; packet size = 4096; pooling = true; Max pool size = 100; min pool size = 1 ", context. parameters ["server"], context. parameters ["dbname"], context. parameters ["user"], context. parameters ["PWD"]);
Foundit = true;
}
}
}
If (! Foundit)
{
Throw new installexception ("the Web. config file does not contain connstring connection string Settings ");
}
Xmldocument. Save (fileinfo. fullname );
}
Catch (exception ex)
{
Throw ex;
}
*/
}
# 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
}
}
I do not need to modify the Web. config section.
Note: If you do not need the SA user to log on to the database, first create a specific SQL user on the server.