C#_ Project Packaging Additional databases

Source: Internet
Author: User

c#_ Project Packaging additional databases

2010-07-11 23:22:45| Category: Winfrom| Report | Font size Subscription

Implementation effect: Attach the database directly when you install the project.

1. first create a new Setup Project in Solution Explorer for the project you want to deploy

2. in the files view of the Setup project, right-click "Application Folder", "Add", "project Output"

Select the startup item in your project, in the list below, the default is main output , OK.

3. in the middle section of the file system, the files required for compiling are automatically listed. A simple project package is almost, there are shortcuts or something, not to say. started to focus.

Creating the Installer Class

4. In Solution Explorer, create a new class library project "Installdb", delete Class1.cs, create a new installer class [InstallDB.cs], Wait for the additional database code to be written in this class.

To create a Custom installation dialog box

5. Right-click on the newly created Setup project, view, user interface:

In the user interface, right-click "Start"-"Add Dialog"-Select "text box (A)"-OK.

6. then right-click this text box (A) and move it up to welcome use below:

Right-click on "Properties" and fill in the information as follows:


7. on the newly created setup project, right-click View, Custom Actions:

8. Right-click the "Install" node of "Custom Action Interface", "Add Custom Action", Pop Up the dialog box.

9. Select the application Folder in the search scope , then click "Add Output (O) ..." On the right, select the newly created Installer class project , default or Primary output, OK. At this time

Right-click this "main output from INSTALLDB (active)", enter the property interface, and enter the following in the "CustomActionData" attribute:

/dbname=[dbname]/server=[server]/user=[user]/pwd=[pwd]/targetdir= "[targetdir]\"

Note: The first four brackets in the uppercase letters, for the 6th step above the input of the four Editproperty properties, need to correspond well. The last Targetdir value means the directory path of the file after installation.

Special reminder: The first three "/xxx=[xxx" behind, there is a space, small Xin has been so vexed for a long time, some of the online tutorials is really ... Very resistant to people.

You can now add the database files, right-click on the newly created installation project, "Add", "File", select your MDF and LDF files, that is, the installation requires additional database files.

Finally, we just need to write the code for the additional database in the Installer class. Open the new Installer class in step 4th above, and refer to the code below to write the additional database code that is appropriate for you:

<summary>
Attaching a Database method
</summary>
<param name= "strSQL" > Connect database string, connect to Master System database </param>
<param name= "Dataname" > Database name </param>
<param name= "strmdf" > Database file MDF path </param>
<param name= "strldf" > Database files LDF path </param>
<param name= "path" > installation directory </param>
private void CreateDatabase (String strSQL, String dataname, String strmdf, String strldf, String path)
{
SqlConnection myconn = new SqlConnection (strSQL);
String str = NULL;
Try
{
str = "EXEC sp_attach_db @dbname = '" +dataname+ "', @filename1 = '" +strmdf+ "', @filename2 = '" +strldf+ "";
SqlCommand mycommand = new SqlCommand (str, myconn);
MyConn.Open ();
Mycommand.executenonquery ();
MessageBox.Show ("Database installation succeeded! Click OK to continue ");//Need using System.Windows.Forms
}
catch (Exception e)
{
MessageBox.Show ("Database installation failed! "+ e.message+" \ n "+" You can manually append data ");
System.Diagnostics.Process.Start (path);//Open the installation directory
}
Finally
{
Myconn.close ();
}
}
public override void Install (System.Collections.IDictionary statesaver)
{
String Server = this. context.parameters["Server"];//server name
String uid = this. context.parameters["User"];//sqlserver username
string pwd = this. context.parameters["pwd"];//password
String path = this. context.parameters["TARGETDIR"];//installation directory
String strSQL = "server=" + server + "uid=" + uid + ";p wd=" + pwd + ";d atabase=master";//Connection database string
String dataname = "JXC";//Database name
String strmdf = path + @ "Jxc.mdf";//mdf file path, note that the filename is the same as the name of the database you just added!
String strldf = path + @ "Jxc_log.ldf";//ldf file path
Base. Install (statesaver);
This. CreateDatabase (strSQL, Dataname, Strmdf, strldf, path);//start creating a database
}
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.