C# winfrom數據庫備份與恢復
C# winfrom數據庫備份與恢復
通過C# winfrom來對SQL數據庫進行數據庫的備份和還原
圖1
圖2
//設置保存的路徑
private void btnBaoCun_Click(object sender, EventArgs e)
{
FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();
//設置根目錄在桌面;
myFolderBrowserDialog.RootFolder = System.Environment.SpecialFolder.Desktop;
//設置當前選擇的路徑
myFolderBrowserDialog.SelectedPath = C:;
//允許在對話框中包括一個新建目錄的按鈕
myFolderBrowserDialog.ShowNewFolderButton = true;
//設置對話框的說明信息
myFolderBrowserDialog.Description = 請選擇輸出目錄;
if (myFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{//確認是否保存
string strLuJing = myFolderBrowserDialog.SelectedPath;//獲取路徑
txtBaoCunBeiFen.Text = strLuJing;//賦值給文本顯示
}
}
///
/// 創建連接字符串
///
///連接地址
///備份的數據庫名稱
///
private static SqlConnection GetConn(string straddress,string SQLname)
{//創建連接字符串與SQL連接,也可以直接調用DAL中的連接
SqlConnection conn = new SqlConnection(@Data Source= + straddress + ;Initial Catalog= + SQLname + ;User ID=sa;Password=123);
return conn;
}
private void btnBaoCunBeiFen_Click(object sender, EventArgs e)
{
if (MessageBox.Show(是否備份數據, 提示, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
if (txtBaoCunBeiFen.Text.ToString() != )
{
//設置連接字符串
SqlConnection conn = GetConn(10.20.0.25:14334,zbwx);
//實例化SQL可執行的存儲過程
SqlCommand cmdBK = new SqlCommand();
//SQL文本
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
{
//進入SQL
conn.Open();
//返回影響行數
cmdBK.ExecuteNonQuery();
MessageBox.Show(備份成功!);
this.Dispose();//釋放資源
this.Close();//關閉
}
catch (Exception)
{
MessageBox.Show(備份失敗);
}
finally
{
conn.Close();//關閉與SQL的連接
}
}
else
{
MessageBox.Show(請選擇保存路徑!);
}
}
}
private void btnHuanYuan_Click(object sender, EventArgs e)
{
//文件控件
OpenFileDialog filename = new OpenFileDialog();
//獲取路徑
filename.InitialDirectory = Application.StartupPath;
//設置可打開的文件格式
filename.Filter = All files (*.*)|*.bak;
filename.FilterIndex = 2;
//是否還原當前路徑
filename.RestoreDirectory = true;
if (filename.ShowDialog() == DialogResult.OK)
{
//處理路徑
string path = filename.FileName.ToString();
string Name = path.Substring(path.LastIndexOf(\) + 1);
txtBaoCunWenJian.Text = path;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show(是否還原數據, 提示, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
if (txtBaoCunWenJian.Text.ToString() != )
{
string databasefile=txtBaoCunWenJian.Text;
MessageBox.Show(databasefile);
if (RestoreDataBase(zbwx, databasefile))
{
MessageBox.Show(還原成功!);
}
else {
MessageBox.Show(還原失敗!);
}
this.Dispose();
this.Close();
}
else
{
MessageBox.Show(請選擇文件路徑!);
}
}
}
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);
///
/// 還原數據庫
///
///需要還原數據庫的名稱
///文件路徑
///
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;//數據庫名稱和路徑 WITH REPLACE是去除日志文件
SqlCommand sqlcmd = new SqlCommand(sql, constring);
sqlcmd.CommandType = CommandType.Text;
try {
//開始
constring.Open();
sqlcmd.ExecuteNonQuery();
}catch(Exception ex)
{
string str = ex.Message;
constring.Close();
return false;
}
constring.Close();//結束
return true;
}