Net Web project installation package (2) database installation and installation of other components

Source: Internet
Author: User

This section describes how to deploy databases and install other components during the installation process.

Run the SQL script to create a database. (You can also use the form of an attachment database file)

You only need to add the SQL script execution method in the Install method to connect to the database using SqlConnection.

Using (SqlConnectionconnection = newSqlConnection (connectionString ))
{
Connection. Open ();

ExecuteSQL (connection, GetResource ("createdatabase. SQL "));

}


/// <Summary>
/// Execute the SQL statement
/// </Summary>
/// <Param name = "connection"> </param>
/// <Param name = "SQL"> </param>
Void ExecuteSQL (SqlConnection connection, string SQL)
{
SqlCommand cmd = new SqlCommand (SQL, connection );
Cmd. ExecuteNonQuery ();
} // <Summary>
/// Obtain the database logon connection string
/// </Summary>
/// <Param name = "databasename"> database name </param>
/// <Returns> </returns>
Private string GetConnectionString (string databasename)
{
Return "server =" + Context. Parameters ["server"]. ToString () + "; database =" + (string. IsNullOrEmpty (databasename )? "Master": databasename) + "; User ID =" + Context. parameters ["user"]. toString () + "; Password =" + Context. parameters ["pwd"]. toString ();
}
Here, we use embedded resources to Package SQL scripts.

Add createdatabase. SQL and dropdatabase. SQL to the project, for example:

 

Right-click createdatabase. SQL to view attributes, for example:

 

Sets the generation operation as an embedded resource. This is also true for dropdatabase. SQL.

Next we will use the code to read the resource script, that is, the GetResource method in the code above.

/// <Summary>
/// Obtain the script in the resource file
/// </Summary>
/// <Param name = "resourceName"> </param>
/// <Returns> </returns>
String GetResource (string resourceName)
{
Assembly ass = Assembly. GetExecutingAssembly ();
Using (Stream stream = ass. GetManifestResourceStream (ass. GetName (). Name + "." + resourceName ))
{
Using (StreamReader reader = new StreamReader (stream, System. Text. Encoding. Default ))
{
Return reader. ReadToEnd ();
}
}
} Now let's look at all the code of the install method, as shown below:

/// <Summary>
/// Install
/// </Summary>
/// <Param name = "stateSaver"> </param>
Public override void Install (IDictionary stateSaver)
{
Base. Install (stateSaver );

String connectionString = GetConnectionString (null );

Try
{
Using (SqlConnection connection = new SqlConnection (connectionString ))
{
Connection. Open ();

ExecuteSQL (connection, GetResource ("createdatabase. SQL "));

}
}
Catch (Exception ex)
{
MessageBox. Show ("Database Installation failed! \ N database configuration error. Please configure the correct information! \ N "+ ex. Message," error! ");

This. Rollback (stateSaver );
}
} Then, the database is created during installation.

To delete a database from uninstallation, you must rewrite the Uninstall method to execute the script in dropdatabase. SQL.

/// <Summary>
/// Uninstall
/// </Summary>
/// <Param name = "savedState"> </param>
Public override void Uninstall (IDictionary savedState)
{
Base. Uninstall (savedState );

/*

// Obtain the saved link string.
String connectionString =

Try
{
Using (SqlConnection connection = new SqlConnection (connectionString ))
{
Connection. Open ();

ExecuteSQL (connection, GetResource ("dropdatabase. SQL "));

}
}
Catch (Exception ex)
{
MessageBox. Show ("failed to uninstall the database! \ N database configuration error. Please configure the correct information! \ N "+ ex. Message," error! ");
}

*/
} A similar operation is performed to create tables and sample data, that is, to package new resources. The database connection of the Web project is in Web. config. Therefore, you must modify the database connection of Web. config during the installation process. Here, a simple replacement is used. As follows :/*
* Set the webconfig connection string
*/
String webconfigpath = Path. Combine (this. Context. Parameters ["targetdir"]. ToString (), "web. config ");
String webcofnigstring = File. ReadAllText (webconfigpath). Replace ("# constring #", GetConnectionString ("hxjdatabasename "));
File. writeAllText (webconfigpath, webcofnigstring); configure <add name = "ConnectionString" connectionString = "# constring #" providerName = "System. data. sqlClient "/> is to replace # constring # With the new link string generated during installation.

 

We also use Microsoft's AJAX library in our OA project, so we need to install the Ajax package.

We package the AJAX package into the installation project.

 

Run the ajax 2.0.msi Code as follows:

/*
* Install the ajax2.0 framework
*/
System. Diagnostics. Process process = new System. Diagnostics. Process ();
Process. StartInfo. FileName = Path. Combine (this. Context. Parameters ["targetdir"]. ToString (), "Ajax 2.0.msi ");
Process. StartInfo. WindowStyle = System. Diagnostics. ProcessWindowStyle. Hidden;
Process. Start ();
Process. WaitForExit (); in this way, the ajax package will be installed during installation.

 

 

Related Article

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.