C # winform packaging database

Source: Internet
Author: User
Effect: attach a database directly when installing a project. 1. First Solution Resource Manager Create a new Installation Project 2. In the file view of the installation project, right-click Program Folder> Add> project output select Start the project In the list below, the default is Primary output , OK. 3. In this case, the files required for compilation are automatically listed in the middle section of the file system. A simple project package is almost the same, and there are shortcuts or something. Start to focus. Create installer class 4. In Solution Explorer, create Class Library Project [installdb], delete class1.cs, and create Installer class [installdb. CS] , Etc. The additional database will be written in this class. Code . Create custom installation dialog box 5. Right-click the newly created installation project and choose View> User Interface. On the user interface, right-click Start> Add dialog box and choose text box) ]-OK. 6. Right-click the text box (A) and select Move up To Welcome Right-click and choose Properties. Enter the information for reference: 7. Right-click the newly created installation project and choose View> Custom operation ]: 8. Right-click the "Install" node on the "Custom operation interface" and click "add custom operation". The displayed dialog box is displayed. 9. In Search range Select Application folder, Click [add output (o)…] on the right )...], Select Installation Program By default, it is still the primary output. OK. At this point: 10. Right-click the [primary output from installdb (activity)] To Go To The properties page. In the customactiondata attribute, enter the following content:
/Dbname = [dbname]/Server = [server]/user = [user]/Pwd = [PWD]/targetdir = "[targetdir] \"
Note: The upper-case letters in the first four square brackets are Four editproperties Attribute. The last targetdir value indicates the directory path of the installed file. Note: The first three"/Xxx = [XXX]"There isSpaceSmall Xin had been so worried for a long time, some online tutorials are really... Very patient. 11. Now you can add a database file. Right-click the newly created installation project, choose add> file, and select your MDF and LDF files, is the database file to be attached during installation. 12. Finally, we only need to write the code for appending the database in the installer class. Open the installation program class created in step 1 above, and refer to the code below to write additional database code suitable for you:

 /// <Summary> ///  Attach database Method  /// </Summary> /// <Param name = "strsql">  Connect the database string to the Master System Database  </Param> /// <Param name = "dataname">  Database Name  </Param> /// <Param name = "strmdf">  Path of the database file MDF  </Param> /// <Param name = "strldf">  Path of database file LDF </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 (  "The database is successfully installed! Click OK to continue"  ); // Using system. Windows. forms required  }  Catch  (  Exception  E ){  MessageBox  .  Show (  "Database Installation failed! "  +  E  .  Message  +  "\ N"  +  "You can manually attach 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  +  "; Pwd ="  +  PWD  +  "; Database = Master"  ;  // Connect to the database string  String  Dataname =  "Jxc"  ;  // Database Name  String  Strmdf  =  Path  +  @ "Jxc. MDF"  ;  // MDF file path. The file name must be the same as the name of the newly added database!  String  Strldf  =  Path  +  @ "Jxc_log.ldf"  ;  // LDF file path Base  .  Install (statesaver );  This  .  Createdatabase (strsql, dataname, strmdf, strldf, PATH );  // Start database creation  } 

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.