


如何使用ASP.NET備份和恢復SqlServer數據庫
首先我們先分析ASP.NET備份SQL數據庫的代碼,詳細如下:
備份SqlServer數據庫:
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "backup database " + this.DropDownList1.SelectedValue + " to disk='" + this.TextBox1.Text.Trim() + ".bak'";
SqlConnection con = new SqlConnection(SqlStr1);
con.Open();
try
{
if (File.Exists(this.TextBox1.Text.Trim()))
{
Response.Write("< language=java>alert('此文件已存在,請從新輸入!');location='Default.aspx'</>");
return;
}
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write("< language=java>alert('備份數據成功!');location='Default.aspx'</>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("< language=java>alert('備份數據失敗!')</>");
}
finally
{
con.Close();
}
下面是ASP.NET還原SQL數據庫的方法,代碼如下:
還原SqlServer數據庫:
string path = this.FileUpload1.PostedFile.FileName; //獲得備份路徑及數據庫名稱
string dbname = this.DropDownList1.SelectedValue;
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "use master restore database " + dbname + " from disk='" + path + "'";
SqlConnection con = new SqlConnection(SqlStr1);
con.Open();
try
{
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write("< language=java>alert('還原數據成功!');location='Default.aspx'</>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("< language=java>alert('還原數據失敗!')</>");
}
finally
{
con.Close();
}
關鍵字:ASP.NET、SqlServer、數據庫
新文章:
- CentOS7下圖形配置網絡的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統后丟失windows啟動項
- CentOS單網卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網打印機IP講解
- CentOS7使用hostapd實現無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網絡重啟出錯
- 解決Centos7雙系統后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統有什么不同呢
- Centos 6.6默認iptable規則詳解