asp.net c#数据库还原

本文介绍了一个用于数据库还原的过程,包括检查备份文件是否存在,打开数据库连接,并执行SQL命令来还原指定的数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public partial class shujuku_huanyuan : System.Web.UI.Page
{
    SqlConnection conn =  new SqlConnection(@"server=HUAZD-33/XXD33;uid=sa;pwd=111111;database=master;");
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlConnection sqlConn = SqlConnections.GetSQLConnection();

            string shujuku = sqlConn.Database;

         
            this.Text1.Value = shujuku;//备份数据库路径
        }
    }
    protected void btnsearch_ServerClick(object sender, EventArgs e)
    {
        ArrayList arraylistsqls = new ArrayList();

        string path1 = Request["FileUpload1"].ToString().Trim(); //this.FileUpload1.FileName.ToString().Trim();

        string shujukuming = this.Text1.Value.ToString().Trim();
      

        if (System.IO.File.Exists(path1))
        {

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('该路径下的备份文件不存在,请重新选择!');</script>");

            return;
        }


        try
        {
            conn.Open();

            SqlCommand commd = new SqlCommand("exec killspid " + shujukuming + "", conn);


            commd.ExecuteNonQuery();

            SqlCommand comm1 = new SqlCommand("use master restore database " + shujukuming + " from disk='" + path1 + "' with replace", conn);
            //SqlCommand comm1 = new SqlCommand("use master restore database aa from disk='" + path1 + "' with replace", conn);

            comm1.ExecuteNonQuery();

            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('数据库还原成功!');window.location.href='huanyuan.aspx';</script>");

        }
        catch (Exception error)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('数据库还原失败!');</script>");

        }

        finally
        {
            conn.Close();
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值