---
title: ".NET备份数据库并压缩 并查看已有备份"
author: "wumingwei-123"
date: "2009-06-28T22:07:00Z"
tags: [".net", "ASP", "ASP.net", "JavaScript", "UI"]
slug: "net"
draft: false
lastmod: "2009-06-28T22:07:00Z"
id: 496856
url: "https://wumingwei.iteye.com/blog/496856"
---

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

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

BackUp.aspx

&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="BackUp.aspx.cs" Inherits="Crm.Pages.Pages\_CRM\_BackUp" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;

&lt;head id="ctl00\_Head1" runat="server"&gt;

&lt;meta name="pragma" content="no-cache"/&gt;

&lt;meta name="cache-control" content="private"/&gt;

&lt;meta http-equiv="EXPIRES" content="0"/&gt;

&lt;link href="../Tcps/Css/MasterPage.css" type="text/css" rel="stylesheet" /&gt;

&lt;title&gt;数据库备份&lt;/title&gt;

&lt;script src="../Tcps/JS/iframe.js" type="text/javascript" &gt;&lt;/script&gt;

&lt;script src="../Tcps/JS/mouse.js" type="text/javascript"&gt;&lt;/script&gt;

&lt;/head&gt;

&lt;body id="MasterPagebody"&gt;

&lt;form runat=server&gt;

&lt;div&gt;

&lt;table width="100%" border="0" cellpadding="2" cellspacing="1" class="border"&gt;

&lt;tr class="title"&gt;

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

&lt;/tr&gt;

&lt;/table&gt;

&lt;br /&gt;

&lt;div style="text-align: left"&gt;

&lt;div&gt;

&lt;table width="100%" class="border"&gt;

&lt;tr class="tdbg" align="left" style="height:100px;"&gt;

&lt;td colspan="5" style="height: 187px" align="center"&gt;

&lt;br /&gt;

&lt;div style="text-align:left"&gt;

&lt;asp:Button ID="Button1" CssClass="Button\_6" runat="server" Text="现在备份" OnClick="Button1\_Click" /&gt;

&lt;asp:Button ID="Button2" CssClass="Button\_6" runat="server" Text="查看备份文件" OnClick="Button2\_Click" /&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

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

&lt;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"&gt;

&lt;br /&gt;

&lt;/div&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;/table&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div&gt;&lt;/div&gt;&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;

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

{

/// &lt;summary&gt;

/// Explain:MCCS数据库备份

///2008-9-8 zcy

/// &lt;/summary&gt;

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&lt;string&gt;)Session\["QX\_List"]);

}

/// &lt;summary&gt;

/// 备份

/// &lt;/summary&gt;

/// &lt;param name="sender"&gt;&lt;/param&gt;

/// &lt;param name="e"&gt;&lt;/param&gt;

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(@"&lt;script type='text/javascript'&gt;alert('数据备份成功')&lt;/script&gt;");

////压缩

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(@"&lt;script type='text/javascript'&gt;alert('压缩成功')&lt;/script&gt;");

}

catch (Exception ex)

{

Response.Write(@"&lt;script type='text/javascript'&gt;alert('" + ex.ToString() + "')&lt;/script&gt;");

}

}

/// &lt;summary&gt;

///

/// &lt;/summary&gt;

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 = "&lt;br /&gt;没有备份文件&lt;br /&gt;&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 = "&lt;br /&gt;";

updatefile.Controls.Add(lbbbb);

}

}

else

{

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

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

lbbbb.Text = savePath.ToString();

updatefile.Controls.Add(lbbbb);

}

}

protected void Button2\_Click(object sender, EventArgs e)

{

showUpDFateFile();

}

}

}

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

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