天天看点

asp.net c#数据库还原

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();

        }

    }

}