參考資料來源:
    https://www.youtube.com/watch?v=-fl7PQ7vpCY&ab_channel=CsharpTutorials
    https://csharp-tutorials1.blogspot.com/2016/03/backup-and-restore-sql-server-database.html

    函式庫:
    using System;
    using System.Windows;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Data;

    聲明數據庫的connectionString:

    1. SqlConnection con = new SqlConnection(Final_window.Properties.Settings.Default.Final_windowConnectionString);

    備份:
    選擇備份路徑(備份到哪一個文件夾中)

    1. private void browseButton_Click(object sender, EventArgs e)
    2. {
    3. FolderBrowserDialog dlg = new FolderBrowserDialog();
    4. if (dlg.ShowDialog() == DialogResult.OK)
    5. {
    6. textBox1.Text = dlg.SelectedPath;
    7. BackupButton.Enabled = true;
    8. }
    9. }

    備份並輸出

    1. private void BackupButton_Click(object sender, EventArgs e)
    2. {
    3. string database = con.Database.ToString();
    4. try
    5. {
    6. if(textBox1.Text==string.Empty)
    7. {
    8. MessageBox.Show("please enter backup file location");
    9. }
    10. else
    11. {
    12. string cmd = "BACKUP DATABASE [" + database + "] TO DISK='" + textBox1.Text + "\\" + "database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";
    13. using(SqlCommand command = new SqlCommand(cmd,con))
    14. {
    15. if(con.State!=ConnectionState.Open)
    16. {
    17. con.Open();
    18. }
    19. command.ExecuteNonQuery();
    20. con.Close();
    21. MessageBox.Show("database backup done successefully");
    22. BackupButton.Enabled = false;
    23. }
    24. }
    25. }
    26. catch
    27. {
    28. }
    29. }

    復原:
    選擇路徑,選擇所要恢復的資料庫bak檔案

    1. private void Browsebutton2_Click(object sender, EventArgs e)
    2. {
    3. OpenFileDialog dlg = new OpenFileDialog();
    4. dlg.Filter = "SQL SERVER database backup files|*.bak";
    5. dlg.Title = "Database restore";
    6. if (dlg.ShowDialog() == DialogResult.OK)
    7. {
    8. textBox2.Text = dlg.FileName;
    9. restoreButton.Enabled = true;
    10. }
    11. }

    復原資料庫

    1. private void restoreButton_Click(object sender, EventArgs e)
    2. {
    3. string database = con.Database.ToString();
    4. if (con.State != ConnectionState.Open)
    5. {
    6. con.Open();
    7. }
    8. try
    9. {
    10. string sqlStmt2 = string.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
    11. SqlCommand bu2 = new SqlCommand(sqlStmt2, con);
    12. bu2.ExecuteNonQuery();
    13. string sqlStmt3 = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + textBox2.Text + "'WITH REPLACE;";
    14. SqlCommand bu3 = new SqlCommand(sqlStmt3, con);
    15. bu3.ExecuteNonQuery();
    16. string sqlStmt4 = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
    17. SqlCommand bu4 = new SqlCommand(sqlStmt4, con);
    18. bu4.ExecuteNonQuery();
    19. MessageBox.Show("database restoration done successefully");
    20. con.Close();
    21. }
    22. catch (Exception ex)
    23. {
    24. MessageBox.Show(ex.ToString());
    25. }
    26. }