ASP.NET中使用代碼來進行備份與還原資料庫

來源:互聯網
上載者:User

 SQL代碼:

 

-- 備份資料庫backup database db_CSManage to disk='c:\backup.bak'-- 還原資料庫,必須先備份該資料庫的記錄檔到原先的備份檔案中backup log db_CSManage to disk='c:\backup.bak'restore database db_CSManage from disk='c:\backup.bak'

 

 

其中db_CSManage是資料庫名稱,disk後的路徑即是備份檔案儲存的路徑。
知道了SQL語句,那麼在.NET代碼中就容易多了,備份的.NET代碼如下:

C#代碼-備份: 

 

try{if (txtPath.Text != "" && txtName122.Text != ""){getSqlConnection geCon = new getSqlConnection();SqlConnection con = geCon.GetCon();string strBacl = "backup database db_CSManage to disk='" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak'";SqlCommand Cmd = new SqlCommand(strBacl, con);if (Cmd.ExecuteNonQuery() != 0){MessageBox.Show("資料備份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);this.Close();}else{MessageBox.Show("資料備份失敗!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);}}else{MessageBox.Show("請填寫備份的正確位置及檔案名稱!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);}// end }catch (Exception ee){MessageBox.Show(ee.Message.ToString());}

 

 

 

以下是還原資料庫的代碼,在樣本中發現開頭得先刪除與該資料庫相關的進程,然後在還原之前得先把資料庫記錄備份到開始的備份檔案中,然後才還原備份檔案,要不然會出錯的,代碼如下:  

C#代碼-還原: 

 

if (textPaht.Text != ""){getSqlConnection geCon = new getSqlConnection();SqlConnection con = geCon.GetCon();if (con.State == ConnectionState.Open){con.Close();}string DateStr = "Data Source=niunan\\sqlexpress;Database=master;User id=sa;PWD=123456";SqlConnection conn = new SqlConnection(DateStr);conn.Open();//-------------------殺掉所有串連 db_CSManage 資料庫的進程--------------string strSQL = "select spid from master..sysprocesses where dbid=db_id( 'db_CSManage') ";SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);DataTable spidTable = new DataTable();Da.Fill(spidTable);SqlCommand Cmd = new SqlCommand();Cmd.CommandType = CommandType.Text;Cmd.Connection = conn;for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++){Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString();   //強行關閉使用者進程 Cmd.ExecuteNonQuery();}conn.Close();conn.Dispose();//--------------------------------------------------------------------SqlConnection sqlcon = new SqlConnection(DateStr);sqlcon.Open();string sql = "backup log db_CSManage to disk='" + textPaht.Text.Trim() + "' restore database db_CSManage from disk='" + textPaht.Text.Trim() + "'";SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);sqlCmd.ExecuteNonQuery();sqlCmd.Dispose();sqlcon.Close();sqlcon.Dispose();MessageBox.Show("資料還原成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);MessageBox.Show("為了必免資料丟失,在資料庫還原後將關閉整個系統。");Application.Exit();}else{MessageBox.Show("請選擇備份檔案!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);}

 

 

轉自:http://niunan.javaeye.com/blog/321062

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.