① 請教各位大神,VB中怎樣備份正在使用中的Access資料庫
當資料庫打開時是不允許用file ,如果你想可以用
Private Declare Function SHFileOperation Lib "shell32.dll " Alias "SHFileOperationA " (lpFileOp As SHFILEOPSTRUCT) As Long
還有一個API函數可以達到這個要求,好像是下面的函數:
Public Declare Function CopyFile Lib "kernel32 " Alias "CopyFileA " (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
可以拷貝已經打開的資料庫文件。
② vb怎麼備份和恢復Access資料庫,最好說清楚一點,我用vs做的項目
access資料庫的備份很簡單,是直接整個文件備份。
把mdb直接復制到新位置改名就完成了備份,恢復的話先斷開所有資料庫連接,刪除原有mdb文件,再把備份文件改名復制回來就行了。
③ 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'>刪除備份資料庫成功!</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"];