ASP.NET中SQL Server数据库备份恢复浅析


  本文标签:SQL Server数据库备份恢复

  ASP.NET中SQL Server数据库备份恢复的操作是如何的呢?首先我们来看看在ASP.NET中是怎么进行SQL Server数据库备份的  。

  以下是进行SQL Server数据库备份引用片段:

  1. string SqlStr1 = "Server=(local);
  2. database=" + this.DropDownList1.SelectedValue + ";
  3. Uid=sa;Pwd=";   
  4. string SqlStr2 = "backup database " + 
  5. this.DropDownList1.SelectedValue + " to disk=" + 
  6. this.TextBox1.Text.Trim() + ".bak";   
  7. SqlConnection con = new SqlConnection(SqlStr1);   
  8. con.Open();   
  9. try   
  10. {   
  11. if (File.Exists(this.TextBox1.Text.Trim()))   
  12. {   
  13. Response.Write(" ");   
  14. return;   
  15. }   
  16. SqlCommand com = new SqlCommand(SqlStr2, con);   
  17. com.ExecuteNonQuery();   
  18. Response.Write(" ");   
  19. }   
  20. catch (Exception error)   
  21. {   
  22. Response.Write(error.Message);   
  23. Response.Write(" ");   
  24. }   
  25. finally   
  26. {   
  27. con.Close();   
  28. }  

  那么在ASP.NET中SQL Server数据库备份之后我们会遇到恢复数据库的操作,下面呢就是SQL Server数据库备份恢复的源码:

  1. string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称   
  2. string dbname = this.DropDownList1.SelectedValue;   
  3. string SqlStr1 = "Server=(local);
  4. database=" + this.DropDownList1.SelectedValue + ";
  5. Uid=sa;Pwd=";   
  6. string SqlStr2 = "use master restore database " + dbname + " from disk=" + path + "";   
  7. SqlConnection con = new SqlConnection(SqlStr1);   
  8. con.Open();   
  9. try   
  10. {   
  11. SqlCommand com = new SqlCommand(SqlStr2, con);   
  12. com.ExecuteNonQuery();   
  13. Response.Write(" ");   
  14. }   
  15. catch (Exception error)   
  16. {   
  17. Response.Write(error.Message);   
  18. Response.Write(" ");   
  19. }   
  20. finally   
  21. {   
  22. con.Close();   
  23. }  

  ASP.NET中SQL Server数据库备份恢复的相关内容就向你介绍到这里,希望对你有所帮助  。