標籤:
Database Backup還原類:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//應用相應的命名空間using System.Windows.Forms;using System.Collections;using System.IO;using System.Data;using System.Data.SqlClient;namespace canyinxt.Command{ public static class BacupDatabase { static string connectionString = "Data Source=(local);Initial Catalog=master;integrated security = true"; static SqlConnection conn = new SqlConnection(connectionString); #region 備份指定的資料庫檔案 /// <summary> /// 備份指定的資料庫檔案 /// </summary> /// <param name="DBName">備份名稱要與資料庫中的資料庫名稱相同</param> /// <param name="databasename">要還原的資料庫(包含要備份的檔案名稱)</param> /// <returns></returns> public static bool BackUpDataBase(string DBName, string databasefile) { if (!File.Exists(databasefile)) { } //還原的資料庫MyDataBase string sql = "BACKUP DATABASE " + "" + DBName + "" + " TO DISK = ‘" + databasefile + ".bak‘ "; conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); comm.CommandType = CommandType.Text; try { comm.ExecuteNonQuery(); } catch (Exception err) { string str = err.Message; conn.Close(); return false; } conn.Close();//關閉資料庫連接 return true; } #endregion //以下是還原資料庫,稍微麻煩些,要關閉所有與當前資料庫相連的串連 #region 還原資料庫 /// <summary> /// 還原資料庫 /// </summary> /// <param name="DBName">要還原資料庫名稱(此名稱要和備份時候的相同)</param> /// <param name="backfile">資料庫檔案路徑(加名稱)</param> /// <returns></returns> public static bool RestoreDatabase(string DBName, string backfile) { ///殺死原來所有的資料庫連接進程 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=(local);Initial Catalog=master;integrated security = true"; conn.Open(); string sql = "SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name=‘" + "" + DBName + "" + "‘"; SqlCommand cmd1 = new SqlCommand(sql, conn); SqlDataReader dr; ArrayList list = new ArrayList(); try { dr = cmd1.ExecuteReader(); while (dr.Read()) { list.Add(dr.GetInt16(0)); } dr.Close(); } catch (SqlException eee) { MessageBox.Show(eee.ToString()); } finally { conn.Close(); } //MessageBox.Show(list.Count.ToString()); for (int i = 0; i < list.Count; i++) { conn.Open(); cmd1 = new SqlCommand(string.Format("KILL {0}", list[i].ToString()), conn); cmd1.ExecuteNonQuery(); conn.Close(); // MessageBox.Show("系統已經清除的資料庫線程: " + list[i].ToString() + "\r\n正在還原資料庫!"); } MessageBox.Show("系統已經清除的資料庫線程: " + list.Count.ToString() + "\r\n正在還原資料庫!"); //這裡一定要是master資料庫,而不能是要還原的資料庫,因為這樣便變成了有其它進程 //佔用了資料庫。 string constr = @"Data Source=(local);Initial Catalog=master;integrated security = true"; string database = DBName; string path = backfile; string BACKUP = String.Format("RESTORE DATABASE {0} FROM DISK = ‘{1}‘ WITH REPLACE", database, path); SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(BACKUP, con); con.Open(); try { cmd.ExecuteNonQuery(); MessageBox.Show("還原成功,點擊退出系統,請重新登入!"); Application.Exit(); return true; } catch (SqlException ee) { //throw(ee); //MessageBox.Show("還原失敗"); MessageBox.Show(ee.ToString() + "還原失敗!", "還原失敗!"); return false; } finally { con.Close(); } } #endregion }}
備份方法的使用:
調用 Command 檔案夾下的 BacupDatabase 類下的 BackUpDataBase(備份方法):
Command.BacupDatabase.BackUpDataBase("DB_CYMS", BackupPath + @"\DB_CYMS")
還原方法的使用:
調用 Command 檔案夾下的 BacupDatabase 類下的 RestoreDatabase(還原方法):
Command.BacupDatabase.RestoreDatabase("DB_CYMS", RestoreDB)
參考:http://www.cnblogs.com/enjoyprogram/p/3177693.html
MSSQL、C# 、Winform、ASP.NET - Database Backup與還原模組