SQL SERVER 資料庫線上備份還原
本範例非離線備份,離線備份常用於ACCESS資料庫,因為ACCESS允許檔案直接複製
資料備份語法
資料還原語法
本文範例
Github Source Code下載
資料備份語法
//資料備份
private void btnDStore_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(txtDSPath.Text.Trim() + ".bak"))
{
MessageBox.Show("該檔案已經存在﹗", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtDSPath.Text = "";
txtDSPath.Focus();
}
else
{
//建立完整備份
//string strSQL = "backup database MMS to disk='" + txtDSPath.Text.Trim() + "MMS.full' " + "with format";
string strSQL = "backup database MMS to disk='" + txtDSPath.Text.Trim() + ".bak'";
Class_SqlClient UseSQL = new Class_SqlClient(); //建立sql連接元件
UseSQL.Edit_Data(strSQL);
MessageBox.Show("數據備份成功﹗", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
資料還原語法
//資料還原
private void btnDRevert_Click(object sender, EventArgs e)
{
try
{
string strSQL = "use master restore database MMS from disk='" + txtDRPath.Text.Trim() + "'WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10, restricted_user";
Class_SqlClient UseSQL = new Class_SqlClient(); //建立sql連接元件
if (UseSQL.Edit_Data(strSQL))
{
MessageBox.Show("數據還原成功﹗", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("數據還原失敗,目前資料庫正在使用中﹗", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
本文範例
Github Source Code下載
留言
張貼留言
您好,我是 Lawrence,這裡是我的開發筆記的網誌,如果你對我的文章有任何疑問或者有錯誤的話,歡迎留言讓我知道。