C # winfrom database backup and recovery

Source: Internet
Author: User

C # winfrom database backup and recovery
C # winfrom database backup and recovery use C # winfrom to back up and restore the SQL database
Figure 1
Figure 2

// Set the saved path private void btnBaoCun_Click (object sender, EventArgs e) {FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog (); // set the root directory on the desktop; myFolderBrowserDialog. rootFolder = System. environment. specialFolder. desktop; // set the current path to myFolderBrowserDialog. selectedPath = C:; // You can include the button myFolderBrowserDialog for creating a directory in the dialog box. showNewFolderButton = true; // description of the Setting Dialog Box myFolderBrowserDialog. description = select the output directory; if (myFolderBrowserDialog. showDialog () = DialogResult. OK) {// confirm whether to save string strLuJing = myFolderBrowserDialog. selectedPath; // obtain the path txtBaoCunBeiFen. text = strLuJing; // value to Text display }}////// Create a connection string //////Connection address ///Backup database name ///
 Private static SqlConnection GetConn (string straddress, string SQLname) {// create a connection string to connect to SQL, you can also directly call the connection SqlConnection conn = new SqlConnection (@ Data Source = + straddress +; Initial Catalog = + SQLname +; User ID = sa; Password = 123) in the DAL ); return conn;} private void btnBaoCunBeiFen_Click (object sender, EventArgs e) {if (MessageBox. show (whether to back up data. The message "MessageBoxButtons" is displayed. OKCancel) = DialogResult. OK) {if (txtBaoC UnBeiFen. Text. ToString ()! =) {// Set the connection string SqlConnection conn = GetConn (10.20.0.25: 14334, zbwx); // instantiate the SQL executable Stored Procedure SqlCommand cmdBK = new SqlCommand (); // SQL text cmdBK. commandType = CommandType. text; cmdBK. connection = conn; // DateTime dtm = new DateTime (); string strRiQi = DateTime. now. year. toString () + (DateTime. now. month. toString (). length <2? 0 + DateTime. Now. Month. ToString (): DateTime. Now. Month. ToString () + (DateTime. Now. Day. ToString (). Length <2? 0 + DateTime. Now. Day. ToString (): DateTime. Now. Day. ToString () + (DateTime. Now. Hour. ToString (). Length <2? 0 + DateTime. now. hour. toString (): DateTime. now. hour. toString () + DateTime. now. minute. toString () + DateTime. now. second. toString (); cmdBK. commandText = @ backup database zbwx to disk = '+ txtBaoCunBeiFen. text ++ strRiQi ++. bak '; try {// enter SQL conn. open (); // The number of affected rows cmdBK. executeNonQuery (); MessageBox. show (Backup successful !); This. dispose (); // release the resource this. close (); // Close} catch (Exception) {MessageBox. show (backup failed);} finally {conn. close (); // Close the connection with SQL} else {MessageBox. show (select the Save path !); }}} Private void btnHuanYuan_Click (object sender, EventArgs e) {// file control OpenFileDialog filename = new OpenFileDialog (); // obtain the path filename. initialDirectory = Application. startupPath; // set the file format to be opened. filter = All files (*. *) | *. bak; filename. filterIndex = 2; // whether to restore the current path filename. restoreDirectory = true; if (filename. showDialog () = DialogResult. OK) {// processing path string path = filename. fileName. toStri Ng (); string Name = path. substring (path. lastIndexOf (\) + 1); txtBaoCunWenJian. text = path ;}} private void button2_Click (object sender, EventArgs e) {if (MessageBox. show (whether to restore data, prompt, MessageBoxButtons. OKCancel) = DialogResult. OK) {if (txtBaoCunWenJian. text. toString ()! =) {String databasefile = txtBaoCunWenJian. Text; MessageBox. Show (databasefile); if (RestoreDataBase (zbwx, databasefile) {MessageBox. Show (restored successfully !);} Else {MessageBox. Show (restoration failed !);} This. Dispose (); this. Close () ;}else {MessageBox. Show (Please select the file path !); }}} Private void frmShuJuBeiFenYuHuiFu_Load (object sender, EventArgs e) {string strMoRen = System. environment. currentDirectory; txtBaoCunBeiFen. text = strMoRen;} SqlConnection constring = new SqlConnection (Data Source = (local); Initial Catalog = master; User ID = sa; Password = 123 );////// Restore the database //////Name of the database to be restored ///File Path ///
 Public bool RestoreDataBase (string databasename, string databasefile) {// SqlConnection constring = new SqlConnection (Data Source = (local); Initial Catalog = master; User ID = sa; password = 123); string SQL = RESTORE DATABASE + databasename + from DISK = '+ databasefile +' + WITH REPLACE; // The Database Name and path with replace is to remove the log file SqlCommand sqlcmd = new SqlCommand (SQL, constring); sqlcmd. commandType = CommandType. text; try {// start constring. open (); sqlcmd. executeNonQuery ();} catch (Exception ex) {string str = ex. message; constring. close (); return false;} constring. close (); // end return true ;}
 

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.