.NET备份数据库并压缩 并查看已有备份

对数据库的备份 和压缩操作等功能 全部集成在一起 被能够查看已经备份并压缩好的文件

 


这是数据库备份的展示页面 代码如下:  

BackUp.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BackUp.aspx.cs" Inherits="Crm.Pages.Pages_CRM_BackUp" %>

 

<!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 id="ctl00_Head1" runat="server">

<meta name="pragma" content="no-cache"/>

         <meta name="cache-control" content="private"/>

         <meta http-equiv="EXPIRES" content="0"/>

<link href="../Tcps/Css/MasterPage.css" type="text/css" rel="stylesheet" />

<title>数据库备份</title>

<script src="../Tcps/JS/iframe.js" type="text/javascript"  ></script>

   <script src="../Tcps/JS/mouse.js" type="text/javascript"></script>

</head>

<body id="MasterPagebody">

<form runat=server>

<div>

  <table width="100%" border="0" cellpadding="2" cellspacing="1" class="border">

        <tr class="title">

            <td align="left"><span style="display:inline-block;margin-left:8px"><span><img src="../Tcps/Images/localGIF.gif" width="17" height="17" align="absmiddle" style="border-width:0px;" /> 您现在的位置:</span> 管理页面&gt;&gt;&gt;  数据库备份</span></td>

        </tr>

    </table>

    <br />

    <div style="text-align: left">

      <div>

     <table width="100%" class="border">

         <tr class="tdbg" align="left" style="height:100px;">

              <td colspan="5" style="height: 187px" align="center">                  

                    <br />

                <div  style="text-align:left">

                    <asp:Button ID="Button1"   CssClass="Button_6" runat="server" Text="现在备份" OnClick="Button1_Click" />

                    <asp:Button ID="Button2"  CssClass="Button_6" runat="server" Text="查看备份文件" OnClick="Button2_Click" /><br />

                 </div>

            </td>

         </tr>

         <tr>

             <td style="padding-left:5px;"> <asp:Label ID="Label1" runat="server" Text="现有备份文件"></asp:Label>

                <div id="updatefile" runat="server" style="padding-left:5px;border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid">                

                   <br />

                  

               </div>

             </td>

         </tr>

     </table>

</div>       

    </div>

      </div>   

<div></div></form>

</body>

</html>

 

Cs代码文件如下:

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Collections.Generic;

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.Data.SqlClient;

using System.IO;

using Model;

using System.Runtime.InteropServices;

using Microsoft.Win32;

using System.Diagnostics;

 

 

namespace Crm.Pages

{

    /// <summary>

    /// Explain:MCCS数据库备份

    ///2008-9-8 zcy 

    /// </summary>

    public partial class Pages_CRM_BackUp : System.Web.UI.Page

    {

        private string page_id = "242";

        protected void Page_Load(object sender, EventArgs e)

        {

            MyPages.Load(this, this.page_id, (List<string>)Session["QX_List"]);

 

        }

        /// <summary>

        /// 备份

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void Button1_Click(object sender, EventArgs e)

        {

            //AppSettingsReader ap = new AppSettingsReader();

            //string SqlStr1 = ap.("FAMConnectionString", String);

 

            SqlConnection con = DBCOM.DBCom.getconn();//获取连接对象的方法

            //sqlpath

            string sqlpath = ConfigurationSettings.AppSettings["sqlpath"];

            string sqlpath2 = ConfigurationSettings.AppSettings["sqlpath2"];

            string name_sqlfile = DateTime.Now.ToString("yyyyMMddHHmmss") ;//名称

            string path = sqlpath +"/"+ name_sqlfile+ ".dat";

            string sqlurl = ConfigurationSettings.AppSettings["sqlurl"];//数据库服务器地址

            // com.CommandText = "BACKUP DATABASE " + textBox1.Text + " TO DISK = '" + textBox2.Text.ToString() + "'";

            //  string SqlStr2 = "use master restore database FAM from disk='" + path + "'";

            SqlCommand com = new SqlCommand("BACKUP DATABASE newsystem TO DISK = '" + path + "'", con);

            con.Open();

            com.ExecuteNonQuery();

            con.Close();

            //con.Dispose();

 

            Response.Write(@"<script type='text/javascript'>alert('数据备份成功')</script>");

 

            压缩

            string savePath = Server.MapPath(ConfigurationSettings.AppSettings["savePath"]);

            String the_rar;

            RegistryKey the_Reg;

            Object the_Obj;

            String the_Info;

            ProcessStartInfo the_StartInfo;

            Process the_Process;

            try

            {

                the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command");

                the_Obj = the_Reg.GetValue("");

                the_rar = the_Obj.ToString();

                the_Reg.Close();

                the_rar = the_rar.Substring(1, the_rar.Length - 7);

                the_Info = " a  " + name_sqlfile + ".rar  " + sqlurl + sqlpath2 + name_sqlfile + ".dat";

                the_StartInfo = new ProcessStartInfo();

                the_StartInfo.FileName = the_rar;

                the_StartInfo.Arguments = the_Info;

                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                the_StartInfo.WorkingDirectory = savePath;//获取或设置要启动的进程的初始目录。

                the_Process = new Process();

                the_Process.StartInfo = the_StartInfo;

                the_Process.Start();

                if (the_Process.WaitForExit(1000))

                {

                    the_Process.Close();

                    the_Process.Dispose();

                }

                Response.Write(@"<script type='text/javascript'>alert('压缩成功')</script>");

            }

            catch (Exception ex)

            {

                Response.Write(@"<script type='text/javascript'>alert('" + ex.ToString() + "')</script>");

            }

 

        }

        /// <summary>

        ///

        /// </summary>

        public void showUpDFateFile()

        {

            string rarName = "";

            string filePathAndName = "";

            string savePath = Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["savePath"]);

           

            updatefile.Controls.Clear();

            Label lbbbb = null;

            if (System.IO.Directory.Exists(savePath))

            {

                string[] ff = Directory.GetFiles(savePath);

 

                if (null == ff || ff.Length == 0)

                {

                    lbbbb = new System.Web.UI.WebControls.Label();

                    lbbbb.Text = "<br />没有备份文件<br />&nbsp;";

                    //lbbbb.Text = savePath.ToString();

                    updatefile.Controls.Add(lbbbb);

                }

                foreach (string file in ff)

                {

                    HtmlAnchor a = new HtmlAnchor();

                    a.HRef = file;

                    a.InnerHtml = file;

                    a.Target = "_blank";

                    updatefile.Controls.Add(a);

 

                    lbbbb = new System.Web.UI.WebControls.Label();

                    lbbbb.Text = "<br />";

                    updatefile.Controls.Add(lbbbb);

                }

            }

            else

            {

                lbbbb = new System.Web.UI.WebControls.Label();

                lbbbb.Text = "<br />没有备份文件<br />&nbsp;";

                lbbbb.Text = savePath.ToString();

                updatefile.Controls.Add(lbbbb);

            }

        }

        protected void Button2_Click(object sender, EventArgs e)

        {

            showUpDFateFile();

        }

}

}

 

 

 里面的功能都已经齐全 如果本服务器可以将某些路径写死 上面的部分路径和变量都有说明

 

如不清楚请留言 或登录 http://www.lvtaostudio.com 查看和留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值