⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 restore.aspx.cs

📁 物业管理系统
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class admin_Default2 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
       
            //if (Convert.ToString(Session["UID"]) == "")
            //{
            //    Response.Redirect("login.aspx");

            //}
           
          if (!IsPostBack)
            {
                this.bind();
            }
        
    }


    ///说明:Directory类方getFiles重载方法参数说明:
    ///public static string[] GetFiles(string path,string searchPattern)
    ///public static string[] Getfiles(stirng path,string searchPattern,SearchOption searchOption)
    ///string path:表示要查找的目录
    ///string searchPattern 表示要与参数path中的文件名区配的查找的字符串
    ///SearchOption SearchOption 是一个SearchOption枚举成员,指定目录包括所有子目录还是仅包括当前目录
    ///SearchOption枚举如下:
    ///AllDirectories:当前目录,以及子目录
    ///TopDirectoryOnly:当前目录

  protected void bind()
    {
        string mydir = Server.MapPath("~/backup") + "\\";//从指定的目录恢复备份


        string extname = "*.bak"; //存放扩展名
       

        foreach (string myfile in Directory.GetFiles(mydir, extname, SearchOption.AllDirectories))
        {


            DropDownList1.Items.Add(Path.GetFileNameWithoutExtension(myfile));//GetFileNameWithoutExtension返回不带扩展名的文件名
          }
        
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        if (restore())
        {
            Label1.Visible = true;
            Label1.Text = "数据库恢复成功!";
           

        }
        else
        {
            Response.Write("<script>alert('数据库恢复失败!');</script>");
            return;
        }

    }
    public bool restore()
    {
         string name = DropDownList1.SelectedItem.ToString();//获得文件名不带扩展名
        string filename = name + ".bak";
        string dir = Server.MapPath("~/backup") + "\\" + filename;//目录
        string sql = "use master restore database SmallHouse from disk='" + dir + "'";
        //Response.Write(sql);
        //Response.End();
        //try
        //{
        //    if (bc.ExecSql(sql))
        //    {
        //        Response.Write("<script>alert('数据库恢复成功!');</script>");
        //        Response.Redirect("backup.aspx");

        //    }
        //}

        //catch (Exception ex)
        //{
        //    Response.Write("<script>alert('恢复失败!失败原因:" + ex.Message + "');</script>");
        //}
            
        //}
        string settings = "Server=.;User id=sa;Pwd=;Database=master";

        
        SqlConnection RestoreCon = new SqlConnection(settings);
        SqlCommand RestoreCmd=new SqlCommand("killspid",RestoreCon);
        RestoreCmd.CommandType=CommandType.StoredProcedure;
        RestoreCmd.Parameters.Add("@dbname",SqlDbType.VarChar,50);
        RestoreCmd.Parameters["@dbname"].Value="smallhouse";
        try
        {
            RestoreCon.Open();
            RestoreCmd.ExecuteNonQuery();
            SqlCommand ResoreCmd1 = new SqlCommand();
            ResoreCmd1.CommandText = sql;
            ResoreCmd1.Connection = RestoreCon;
            ResoreCmd1.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            Response.Write(ex);
            Response.End();
            
            return false;
        }
        finally
        {
            RestoreCon.Close();
        }

    }
 
}  


        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -