如何使用ASP.NET備份和恢復(fù)SqlServer數(shù)據(jù)庫
首先我們先分析ASP.NET備份SQL數(shù)據(jù)庫的代碼,詳細如下:
備份SqlServer數(shù)據(jù)庫:
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('備份數(shù)據(jù)成功!');location='Default.aspx'</>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("< language=java>alert('備份數(shù)據(jù)失敗!')</>");
}
finally
{
con.Close();
}
下面是ASP.NET還原SQL數(shù)據(jù)庫的方法,代碼如下:
還原SqlServer數(shù)據(jù)庫:
string path = this.FileUpload1.PostedFile.FileName; //獲得備份路徑及數(shù)據(jù)庫名稱
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('還原數(shù)據(jù)成功!');location='Default.aspx'</>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("< language=java>alert('還原數(shù)據(jù)失敗!')</>");
}
finally
{
con.Close();
}
關(guān)鍵字:ASP.NET、SqlServer、數(shù)據(jù)庫
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機IP講解
- CentOS7使用hostapd實現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認(rèn)iptable規(guī)則詳解