写写程序,一种成就的感觉

写写程序,一种成就的感觉

导航

备份,还原数据库(Access)

Posted on 2010-01-15 13:50  hateyoucode  阅读(591)  评论(0编辑  收藏  举报

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BackData.aspx.cs" Inherits="Admin_BackData" %>
<%@ Register Src="../UserControl/CheckAdmin.ascx" TagName="CheckAdmin" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
 <link href="Inc/css.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript">
 function checkform()
 {
  if($("TextBox1").value.length==0)
  {
   alert('请填写备份名称');
   return false;
  }
 }
 function $(id)
 {
  return document.getElementById(id);
 }
 </script>
</head>
<body>
    <form id="form1" runat="server">
      <table width="90%" border="0" align="center" cellpadding="2" cellspacing="1" class="table_southidc">
        <tr>
          <td height="30" colspan="2" align="center" class="textbold back_southidc">数据库备份</td>
        </tr>
        <tr>
          <td width="28%" height="30" style="text-align:right;" class="tr_southidc">备份数据库名称:</td>
          <td width="72%" class="text_td_item"><asp:TextBox ID="TextBox1" runat="server" /></td>
        </tr>
        <tr>
          <td height="30" colspan="2" class="tr_southidc"><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="备份数据库" /></td>
        </tr>
      </table>
      <br>
      <br>
      <table width="90%" border="0" align="center" cellpadding="2" cellspacing="1" class="table_southidc">
        <tr>
          <td height="30" colspan="3" align="center" class="textbold back_southidc">数据库还原<asp:Literal ID="Literal2" runat="server"></asp:Literal></td>
        </tr>
        <tr>
          <td width="40%" height="30" class="tr_southidc">备份数据库日期:</td>
          <td width="27%" class="tr_southidc">还原</td>
          <td width="33%" class="tr_southidc">删除</td>
        </tr>
        <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_Command">
        <ItemTemplate>
        <tr>
          <td width="40%" height="30" class="tr_southidc">
              <asp:Literal ID="Literal1" runat="server" Text='<%# GetDataItem() %>'></asp:Literal></td>
          <td width="27%" class="tr_southidc">
              <asp:LinkButton ID="LinkButton1" runat="server" Text="还原" CommandName='HuanYuan' CommandArgument='<%# GetDataItem() %>'></asp:LinkButton></td>
          <td width="33%" class="tr_southidc">
              <asp:LinkButton ID="LinkButton2" runat="server" Text="删除" CommandName='ShanChu' CommandArgument='<%# GetDataItem() %>'></asp:LinkButton></td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
      </table>
    </form>
</body>
</html>

 

 

 

 

 

 

 

 

using System;
using System.IO;
using System.Web.UI.WebControls;

public partial class Admin_BackData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button1.Attributes.Add("onclick", "return checkform();");
            TextBox1.Text = DateTime.Now.ToShortDateString();
            databind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        File.Copy(Server.MapPath("../App_Data/Site.mdb"), Server.MapPath("../App_Data/" + TextBox1.Text.Trim() + ".mdb"), true);
        Common.Message("<script>alert('备份成功');location.href='BackData.aspx'</script>");
    }

    protected void Repeater1_Command(object sender, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string strdate = e.CommandArgument.ToString();
            string strcom = e.CommandName;
            switch (strcom)
            {
                case "HuanYuan":
                    File.Copy(Server.MapPath("../App_Data/" + strdate + ".mdb"), Server.MapPath("../App_Data/Site.mdb"), true);
                    break;
                case "ShanChu":
                    File.Delete(Server.MapPath("../App_Data/" + strdate + ".mdb"));
                    break;
            }
            Common.Message("<script>alert('操作完成');location.href='BackData.aspx'</script>");
        }
    }

    protected void databind()
    {
        string[] s = Directory.GetFiles(Server.MapPath("../App_Data/"));
        string fullname = string.Empty;
        for (int i = 0; i < s.Length; i++)
        {
            string filename = s[i].Substring(s[i].LastIndexOf("\\") + 1);
            string noexfile = filename.Substring(0, filename.LastIndexOf("."));
            noexfile = noexfile.Replace("Site", "");
            if (noexfile != string.Empty)
            {
                if (fullname != string.Empty)
                    fullname = fullname + "," + noexfile;
                else
                    fullname = noexfile;
            }
        }
        if (fullname != string.Empty)
        {
            string[] b = fullname.Split(new char[] { ',' });
            Repeater1.DataSource = b;
            Repeater1.DataBind();
        }
    }
}