C# asp.net 把sql2000的某个数据库备份和还原,怎么写?
单击button1就备份数据库,单击button2就还原数据库,效果要如图中显示的人工操作一样。。其实也不用,可以实现整个数据库备份和还原就行了!数据库备份在E盘下,名称...
单击button1就备份数据库,单击button2就还原数据库,效果要如图中显示的人工操作一样。。其实也不用,可以实现整个数据库备份和还原就行了!数据库备份在E盘下,名称为系统时间就行。。数据库名称叫做emr。。求大虾了。。
展开
1个回答
展开全部
备份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(" ");
return;
}
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write(" ");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write(" ");
}
finally
{
con.Close();
}
还原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(" ");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write(" ");
}
finally
{
con.Close();
}
以下是引用片段:
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(" ");
return;
}
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write(" ");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write(" ");
}
finally
{
con.Close();
}
还原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(" ");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write(" ");
}
finally
{
con.Close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询