VB如何远程备份sqlserver数据库到本地机中

VB如何远程备份sqlserver数据库到本地机中,第1张

(2)远程共享某个目录(3)将远程数据备份文件拷贝到本地机中(4)清除远程共享的目录这里主要用到的是sqlserver中内部的函1.清除老的c:/temp2的文件,并新建c:/temp2'文件cnn0.Execute exec master..xp_cmdshell 'rd c:/temp2'cnn0.Execute exec master..xp_cmdshell 'md c:/temp2'2.共享目录:cnn0.Execute exec master..xp_cmdshell 'net share SQLDATABACK2=C:/temp2'3.备份文件cnn0.Execute backup database &s_db_name & to disk='c:/temp2/ &Date &.dat'4.远程复制到指定目录fso.copyfile // &serverName &/SQLDATABACK2/ &Date &.dat, Trim(Text1), True5.清理文件。

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.Text

using _5dRss.Const

using _5dRss.lib.Data.Tool

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

{

protected void Page_Load(object sender, EventArgs e)

{

//生成备份文件列表

ListBackupFiles()

if (!Page.IsPostBack)

{

Showmsg.Visible = false

//数据库路径

lilDBPath.Text = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"]

//新建一个 FileInfo 对象,并获得数据库文件的大小,然后转换单位为KB

FileInfo myFileInfo = new FileInfo(lilDBPath.Text)

lilDBSize.Text = Convert.ToString(myFileInfo.Length/1024) + " KB"

//如果两个参数都不为空,则继续执行

if (Request.QueryString["cmd"] != null &&Request.QueryString["source"] != null)

{

//备份数据库原文件名

string sourceFileName = Request.QueryString["source"]

//如果 cmd 参数为 DelFile

if (Request.QueryString["cmd"].Equals("DelFile"))

{

//删除备份数据库文件

File.Delete(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\" + sourceFileName)

//刷新备份文件列表

ListBackupFiles()

Showmsg.Visible = true

Showmsg.Text = "<div align='center' style='margin-bottom:8px'><img src='images/aL.gif' style='margin-bottom:-6px'/><span class='alertTxt'>删除备份数据库成功!</span><img src='images/aR.gif' style='margin-bottom:-6px'/></div>"

}

//如果 cmd 参数为 Restore

if (Request.QueryString["cmd"].Equals("Restore"))

{

//用备份文件覆盖原文件

File.Copy(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\" + sourceFileName, HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"], true)

//刷新备份文件列表

ListBackupFiles()

Showmsg.Visible = true

Showmsg.Text = "<div align='center' style='margin-bottom:8px'><img src='images/aL.gif' style='margin-bottom:-6px'/><span class='alertTxt'>还原备份数据库成功!</span><img src='images/aR.gif' style='margin-bottom:-6px'/></div>"

}

}

}

}

protected void lnkbtnCompactDB_Click(object sender, EventArgs e)

{

//压缩修复数据库

AccessDBtool.CompactAccessDB(SysConfig.ConnectionString, HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"])

Showmsg.Visible = true

Showmsg.Text = "<div align='center' style='margin-bottom:8px'><img src='images/aL.gif' style='margin-bottom:-6px'/><span class='alertTxt'>压缩修复数据库成功!</span><img src='images/aR.gif' style='margin-bottom:-6px'/></div>"

}

protected void lnkbtnBackupDB_Click(object sender, EventArgs e)

{

string sourceFileName = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"]

string destFileName = HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\" + "Backup_"

destFileName += DateTime.Now.ToString("yyyyMMddHHmmss")

destFileName += ".mbk"

//将数据库文件Copy到Backup目录,如果有重名文件就覆盖原文件

File.Copy(sourceFileName, destFileName, true)

//生成备份文件列表

ListBackupFiles()

Showmsg.Visible = true

Showmsg.Text = "<div align='center' style='margin-bottom:8px'><img src='images/aL.gif' style='margin-bottom:-6px'/><span class='alertTxt'>备份数据库成功!</span><img src='images/aR.gif' style='margin-bottom:-6px'/></div>"

}

/// <summary>

/// 生成备份文件列表

/// </summary>

/// <returns>文件列表,文件详细信息及 *** 作选项的HTML代码</returns>

public void ListBackupFiles()

{

//如果目录不存在则创建次目录

if (!Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\"))

Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\")

DirectoryInfo mydir = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\")

StringBuilder sb = new StringBuilder()

foreach (FileInfo f in mydir.GetFiles())

{

sb.Append("<a href='backup/" + f.Name + "' target='_blank'><img border='0' src='images/mdb.gif' style='margin:4px 3px -3px 0px'/>" + f.Name + "</a> <a href='?cmd=DelFile&source=" + f.Name + "' title='删除备份文件'>删除</a>| <a href='?cmd=Restore&source=" + f.Name + "' title='删除备份文件'>还原数据库</a>| " + f.Length/1024 + " KB | " + f.CreationTime + "<br />")

}

lilBackupFileList.Text = sb.ToString()

}

}

把下面这句换成你的数据库地址:

//数据库路径

// lilDBPath.Text = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9923279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存