protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath("") + @"/Backup";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try
{
path = Convert.ToString(Hst.DBUtility.DbHelperSQL.GetSingle("p_DataBaseBack"));
LabelMessage.Text = "备份成功; 文件存放路径在:" + path;
}
catch
{
LabelMessage.Text = "备份失败!请联系管理员";
}
}
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.Data.SqlClient;
using Hst.DBUtility;
public partial class SysManager_DataBack : System.Web.UI.Page
{
const string FilePath = "E://Backup//";
int a = 1;
Hst.SysManage.FileControl fc = new Hst.SysManage.FileControl();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Acurity.AcurityCheck.CheckPower(308, Page, 1);
}
Bind();
}
void Bind()
{
Hst.SysManage.FileControl fc = new Hst.SysManage.FileControl();
DataList1.DataSource = fc.GetFileList(FilePath, "bak");
DataList1.DataKeyField = "fileName";
DataList1.DataBind();
}
protected void Submit1_ServerClick(object sender, EventArgs e)
{
string name = Request.Form["radiobutton"];
if (name != null)
{
string backupstr = "use master exec killspid Data_Hst restore DATABASE Data_Hst from disk='" + FilePath.Trim() + name.Trim() + "' with replace;";
bool result = fc.ExecuteSql(backupstr);
if (result)
{
Hst.Common.MessageBox.Show(Page, "恢复成功!");
}
else
{
Hst.Common.MessageBox.Show(Page, "恢复失败!请联系管理员!");
}
}
else
{
Hst.Common.MessageBox.Show(Page, "请选择一个文件!");
}
}
protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
a = e.NewPageIndex;
AspNetPager1.CurrentPageIndex = a;
Bind();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace Hst.SysManage
{
public class FileControl
{
///
/// 获取指定文件夹内文件列表
///
/// 父级文件夹路径
/// 指定要查找的子文件的文件类型
///
public DataTable GetFileList(string FolderPath, string FileType)
{
DirectoryInfo Dic;
Dic = new DirectoryInfo(FolderPath);//根据父文件夹路径检索子文件夹或文件。
FileInfo[] dirs = Dic.GetFiles();//将子文件路径存入数组
DataTable lic = new DataTable();
//添加列标题
lic.Columns.Add("fileName");
lic.Columns.Add("fileSize");
lic.Columns.Add("BackTime");
DataRow li;
if (dirs.Length > 0)
{
foreach (FileInfo file in dirs)
{
if (file.Name.Split(new char[] { '.' }).Length > 1 && file.Name.Split(new char[] { '.' })[1].ToLower() == FileType)
{
li = lic.NewRow();
li["fileName"] = file.Name;
li["fileSize"] = CalculateSize(file.Length);
li["BackTime"] = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
lic.Rows.Add(li);
}
}
}
return lic;
}
///
/// 文件大小格式转换
///
/// 文件大小
///
private string CalculateSize(long size)
{
string length = string.Empty;
if (size < 1024)
{
length = size + "bytes";
}
else
if (size < 1024 * 1024)
{
length = float.Parse((size * 10 / 1024).ToString()) / 10 + "KB";
}
else
if (size < 1024 * 1024 * 1024)
{
length = float.Parse((size * 10 / 1048576).ToString()) / 10 + "MB";
}
else
{
length = float.Parse((size * 10 / 1073741824).ToString()) / 10 + "GB";
}
return length;
}
///
/// creatime:2010-3-1 11
///
/// sql语句
///
public bool ExecuteSql(string SQLString)
{
string connectionString = Hst.DBUtility.PubConstant.ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (System.Data.SqlClient.SqlException e)
{
return false;
connection.Close();
throw e;
}
}
}
}
}
}
USE [master]
GO
/****** Object: StoredProcedure [dbo].[killspid] Script Date: 05/19/2010 12:57:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create proc [dbo].[killspid] (@dbname varchar(20))
as
begin
declare @sql nvarchar(500)
declare @spid int
set @sql='declare getspid cursor for
select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status < >-1
begin
exec('kill '+@spid)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end