我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

vb.net备份sql的简单介绍

VB.NET备份SQL数据库

using System;

成都创新互联从2013年开始,先为单县等服务建站,单县等地企业,进行企业商务咨询服务。为单县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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'删除备份数据库成功!/spanimg 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'还原备份数据库成功!/spanimg 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'压缩修复数据库成功!/spanimg 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'备份数据库成功!/spanimg 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=DelFilesource=" + f.Name + "' title='删除备份文件'删除/a | a href='?cmd=Restoresource=" + 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"];

vb.net中备份SQL数据库问题

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'删除备份数据库成功!/spanimg 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'还原备份数据库成功!/spanimg 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'压缩修复数据库成功!/spanimg 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'备份数据库成功!/spanimg 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=DelFilesource=" + f.Name + "' title='删除备份文件'删除/a | a href='?cmd=Restoresource=" + 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"];

使用VB.net实现sql数据库的备份与恢复代码

Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 备份数据库 BACKUP DATABASE Test TO DISK= C:\Backup WITH Format Dim cn As SqlConnection Dim cm As SqlCommand cn = getDB() cm = New SqlCommand( BACKUP DATABASE JXbase TO DISK= C:\Backup WITH Format cn) cn Open() cm ExecuteNonQuery() MsgBox( 备份成功 ) cn Close()

End Sub

恢复 Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click Dim cn As New SqlConnection Dim cn As New SqlConnection Dim mydr As SqlDataReader Dim str As String

Dim tmpConnectionString As String = Data Source=JJK;Initial Catalog=master;Integrated Security=True cn ConnectionString = tmpConnectionString cn ConnectionString = tmpConnectionString cn Open() cn Open() Dim cm As SqlCommand = New SqlCommand( select spid from master sysprocesses where dbid=db_id( JXbase ) cn) mydr = cm ExecuteReader()

Dim cm As SqlCommand = New SqlCommand() cm Connection = cn

While (mydr Read()) 杀死进程

str = kill mydr( spid ) ToString() cm CommandText = str cm CommandType = CommandType Text Application DoEvents() cm ExecuteNonQuery() 杀进程 End While mydr Close() cm = New SqlCommand( RESTORE DATABASE JXbase FROM DISK= C:\Backup cn) 恢复备份 cm ExecuteNonQuery() MsgBox( 恢复成功成功 ) cn Close() cn Close()

lishixinzhi/Article/program/net/201311/12901


本文名称:vb.net备份sql的简单介绍
本文网址:http://mswzjz.cn/article/dodggpp.html

其他资讯