監(jiān)理公司管理系統(tǒng) | 工程企業(yè)管理系統(tǒng) | OA系統(tǒng) | ERP系統(tǒng) | 造價咨詢管理系統(tǒng) | 工程設(shè)計管理系統(tǒng) | 甲方項目管理系統(tǒng) | 簽約案例 | 客戶案例 | 在線試用
X 關(guān)閉

C#實現(xiàn)數(shù)據(jù)庫備份和還原

申請免費試用、咨詢電話:400-8352-114

    本文主要介紹如何利用c#實現(xiàn)數(shù)據(jù)庫的備份和還原: 1、數(shù)據(jù)庫備份操作  if (MessageBox.Show("確定要備份數(shù)據(jù)庫嗎?", "提示框", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                saveFileDialog1.Filter = "數(shù)據(jù)庫文件|*.bak";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string path = saveFileDialog1.FileName;
                    if (File.Exists(path))
                    {
                        File.Delete(path);//注意,這個步驟很重要,如果重復(fù),在備份的數(shù)據(jù), 就會變成,                         //你剛開始的數(shù)據(jù),所以每次都要先刪除.                     }
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);                         fs.Close();
                    }
                    string backupstr = "backup database SMDB to disk='" + path + "';";
                    bool re = SQL.sqlcon(backupstr);//執(zhí)行語句
                  
                    MessageBox.Show("備份成功!");
                  
                }             } 2、數(shù)據(jù)庫還原操作:  if (MessageBox.Show("確定要還原數(shù)據(jù)庫嗎?", "提示框", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                openFileDialog1.Filter = "數(shù)據(jù)庫文件|*.bak";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string path = openFileDialog1.FileName;
                    string backupstr = "use master ";
                    string s1 = " ALTER DATABASE SMDB SET OFFLINE WITH ROLLBACK IMMEDIATE";
                    string s2 = " Restore Database SMDB From disk='" + path + "' with REPLACE";
                    string s3 = " ALTER DATABASE SMDB SET ONLINE WITH ROLLBACK IMMEDIATE";
                    string sSql = backupstr + s1 + s2 + s3;
                    SQL.sqlcon(sSql);//執(zhí)行語句                 
                    MessageBox.Show("恢復(fù)成功!");
                  
                }
            }  
發(fā)布:2007-03-30 10:24    編輯:泛普軟件 · xiaona    [打印此頁]    [關(guān)閉]