protected void bfsjk_Click(object sender, EventArgs e)
{
string nowTime = DateTime.Now.ToString("yyyy-M-dd H:m:s");
string newname = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒") + ".bak";
string ConString = System.Configuration.ConfigurationManager.AppSettings["SourceDataLink"].ToString();
SqlConnection conn = new SqlConnection(ConString);
conn.Open();
//SqlCommand cmd = new SqlCommand("backup database [B1232] to disk='E:/wwwroot/B1230/" + newname + "'", conn);
SqlCommand cmd = new SqlCommand("backup database [B1232] to disk='" + Server.MapPath("~/" + newname) + "'", conn);
string sqlinsert = "insert into adminDatabase (dataName,dataTime)values('" + newname + "','" + nowTime + "')";
SqlCommand cmdinser = new SqlCommand(sqlinsert, conn);
//string sqlinsert = "insert into adminDatabase (dataName,dataTime)values('" + newname + "'," + nowTime + ") ";
cmdinser.ExecuteNonQuery();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
MessageBox("备份成功!");
}
protected void Button2_Click(object sender, EventArgs e)
{
// + this.FileUpload1.PostedFile.FileName
string path = "~/"; //获得备份路径及数据库名称
string dbname = this.DropDownList1.SelectedItem.Text.ToString();
string ConString = System.Configuration.ConfigurationManager.AppSettings["SourceDataLink"].ToString();
SqlConnection conn = new SqlConnection(ConString);
conn.Open();
//string SqlStr2 = "use master restore database B1232 from disk='E:/wwwroot/B1230/" + dbname + "' with replace use [B1232]";
string SqlStr2 = "use master restore database B1232 from disk='" + Server.MapPath(path) + dbname + "' with replace use [B1232]";
//"use master Restore database [xcsrj] from disk='"&NewPath&"' with replace use [xcsrj]"B1219
try
{
SqlCommand cmd = new SqlCommand("master.dbo.killspid", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter spr = new SqlParameter("@dbname", SqlDbType.VarChar, 20);
spr.Value="B1232";
cmd.Parameters.Add(spr);
cmd.ExecuteNonQuery();
SqlCommand com = new SqlCommand(SqlStr2, conn);
com.ExecuteNonQuery();
MessageBox("还原" + dbname + "备份成功!");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('还原数据失败!')</script>");
}
finally
{
conn.Close();
}
备份还原文件的方法
最新推荐文章于 2024-02-07 22:09:49 发布