如果你使用的SQL
Server資料庫,那麼可以在SQL
Server中編寫過程,來實現備份,然後再VB中調用。
或者使用上面朋友補充裡面的Backup
T-SQL語句。當然他使用的是VB.NET語法,在ADO中應該同樣被支持。
『貳』 用VB.NET 和SQL Server2000做項目,請問,怎麼實現資料庫的備份和還原。謝謝
SQL的備份語句
備份整個資料庫:
BACKUP DATABASE { database_name | @database_name_var }
TO < backup_device > [ ,...n ]
[ WITH
[ BLOCKSIZE = { blocksize | @blocksize_variable } ]
[ [ , ] DESCRIPTION = { 'text' | @text_variable } ]
[ [ , ] DIFFERENTIAL ]
[ [ , ] EXPIREDATE = { date | @date_var }
| RETAINDAYS = { days | @days_var } ]
[ [ , ] PASSWORD = { password | @password_variable } ]
[ [ , ] FORMAT | NOFORMAT ]
[ [ , ] { INIT | NOINIT } ]
[ [ , ] MEDIADESCRIPTION = { 'text' | @text_variable } ]
[ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
[ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
[ [ , ] NAME = { backup_set_name | @backup_set_name_var } ]
[ [ , ] { NOSKIP | SKIP } ]
[ [ , ] { NOREWIND | REWIND } ]
[ [ , ] { NOUNLOAD | UNLOAD } ]
[ [ , ] RESTART ]
[ [ , ] STATS [ = percentage ] ]
]
備份特定的文件或文件組:
BACKUP DATABASE { database_name | @database_name_var }
< file_or_filegroup > [ ,...n ]
TO < backup_device > [ ,...n ]
[ WITH
[ BLOCKSIZE = { blocksize | @blocksize_variable } ]
[ [ , ] DESCRIPTION = { 'text' | @text_variable } ]
[ [ , ] DIFFERENTIAL ]
[ [ , ] EXPIREDATE = { date | @date_var }
| RETAINDAYS = { days | @days_var } ]
[ [ , ] PASSWORD = { password | @password_variable } ]
[ [ , ] FORMAT | NOFORMAT ]
[ [ , ] { INIT | NOINIT } ]
[ [ , ] MEDIADESCRIPTION = { 'text' | @text_variable } ]
[ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
[ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
[ [ , ] NAME = { backup_set_name | @backup_set_name_var } ]
[ [ , ] { NOSKIP | SKIP } ]
[ [ , ] { NOREWIND | REWIND } ]
[ [ , ] { NOUNLOAD | UNLOAD } ]
[ [ , ] RESTART ]
[ [ , ] STATS [ = percentage ] ]
]
備份一個事務日誌:
BACKUP LOG { database_name | @database_name_var }
{
TO < backup_device > [ ,...n ]
[ WITH
[ BLOCKSIZE = { blocksize | @blocksize_variable } ]
[ [ , ] DESCRIPTION = { 'text' | @text_variable } ]
[ [ ,] EXPIREDATE = { date | @date_var }
| RETAINDAYS = { days | @days_var } ]
[ [ , ] PASSWORD = { password | @password_variable } ]
[ [ , ] FORMAT | NOFORMAT ]
[ [ , ] { INIT | NOINIT } ]
[ [ , ] MEDIADESCRIPTION = { 'text' | @text_variable } ]
[ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
[ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
[ [ , ] NAME = { backup_set_name | @backup_set_name_var } ]
[ [ , ] NO_TRUNCATE ]
[ [ , ] { NORECOVERY | STANDBY = undo_file_name } ]
[ [ , ] { NOREWIND | REWIND } ]
[ [ , ] { NOSKIP | SKIP } ]
[ [ , ] { NOUNLOAD | UNLOAD } ]
[ [ , ] RESTART ]
[ [ , ] STATS [ = percentage ] ]
]
}
< backup_device > ::=
{
{ logical_backup_device_name | @logical_backup_device_name_var }
|
{ DISK | TAPE } =
{ 'physical_backup_device_name' | @physical_backup_device_name_var }
}
< file_or_filegroup > ::=
{
FILE = { logical_file_name | @logical_file_name_var }
|
FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var }
}
截斷事務日誌:
BACKUP LOG { database_name | @database_name_var }
{
[ WITH
{ NO_LOG | TRUNCATE_ONLY } ]
}
參數
DATABASE
指定一個完整的資料庫備份。假如指定了一個文件和文件組的列表,那麼僅有這些被指定的文件和文件組被備份。
說明 在進行完整資料庫備份或差異資料庫備份時,Microsoft® SQL Server™ 備份足夠的事務日誌,以生成一個將在還原資料庫時使用的一致的資料庫。在 master 資料庫上只能採用完整資料庫備份。
{ database_name | @database_name_var }
指定了一個資料庫,從該資料庫中對事務日誌、部分資料庫或完整的資料庫進行備份。如果作為變數 (@database_name_var) 提供,則可將該名稱指定為字元串常量 (@database_name_var = database name) 或字元串數據類型(ntext 或 text 數據類型除外)的變數。
< backup_device >
指定備份操作時要使用的邏輯或物理備份設備。可以是下列一種或多種形式:
{ logical_backup_device_name } | { @logical_backup_device_name_var }
是由 sp_admpdevice 創建的備份設備的邏輯名稱,資料庫將備份到該設備中,其名稱必須遵守標識符規則。如果將其作為變數 (@logical_backup_device_name_var) 提供,則可將該備份設備名稱指定為字元串常量 (@logical_backup_device_name_var = logical backup device name) 或字元串數據類型(ntext 或 text 數據類型除外)的變數。
{ DISK | TAPE } =
'physical_backup_device_name' | @physical_backup_device_name_var
允許在指定的磁碟或磁帶設備上創建備份。在執行 BACKUP 語句之前不必存在指定的物理設備。如果存在物理設備且 BACKUP 語句中沒有指定 INIT 選項,則備份將追加到該設備。
當指定 TO DISK 或 TO TAPE 時,請輸入完整路徑和文件名。例如,DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Mybackup.dat' 或 TAPE = '\\.\TAPE0'。
說明 對於備份到磁碟的情況,如果輸入一個相對路徑名,備份文件將存儲到默認的備份目錄中。該目錄在安裝時被設置並且存儲在 KEY_LOCAL_MACHINE\Software\Microsoft\MSSQLServer\MSSQLServer 目錄下的 BackupDirectory 注冊表鍵值中。
如果使用的是具有統一命名規則 (UNC) 名稱的網路伺服器或已重新定向的驅動器號,則請指定磁碟的設備類型。
當指定多個文件時,可以混合邏輯文件名(或變數)和物理文件名(或變數)。但是,所有的設備都必須為同一類型(磁碟、磁帶或管道)。
Windows 98 不支持備份到磁碟。
n
是表示可以指定多個備份設備的佔位符。備份設備數目的上限為 64。
BLOCKSIZE = { blocksize | @blocksize_variable }
用位元組數來指定物理塊的大小。在 Windows NT 系統上,默認設置是設備的默認塊大小。一般情況下,當 SQL Server 選擇適合於設備的塊大小時不需要此參數。在基於 Windows 2000 的計算機上,默認設置是 65,536(64 KB,是 SQL Server 支持的最大大小)。
對於磁碟,BACKUP 自動決定磁碟設備合適的塊大小。
說明 如果要將結果備份集存儲到 CD-ROM 中然後從 CD-ROM 中恢復,請將 BLOCKSIZE 設為 2048。
磁帶的默認 BLOCKSIZE 為 65,536 (64 KB)。顯式聲明塊大小將替代 SQL Server 選擇的塊大小。
DESCRIPTION = { 'text' | @text_variable }
指定描述備份集的自由格式文本。該字元串最長可以有 255 個字元。
DIFFERENTIAL
指定資料庫備份或文件備份應該與上一次完整備份後改變的資料庫或文件部分保持一致。差異備份一般會比完整備份佔用更少的空間。對於上一次完整備份時備份的全部單個日誌,使用該選項可以不必再進行備份。有關更多信息,請參見差異資料庫備份和文件差異備份。
說明 在進行完整資料庫備份或差異備份時,SQL Server 備份足夠的事務日誌,從而在恢復資料庫時生成一個一致的資料庫。
EXPIREDATE = { date | @date_var }
指定備份集到期和允許被重寫的日期。如果將該日期作為變數 (@date_var) 提供,則可以將該日期指定為字元串常量 (@date_var = date)、字元串數據類型變數(ntext 或 text 數據類型除外)、smalldatetime 或者 datetime 變數,並且該日期必須符合已配置的系統 datetime 格式。
RETAINDAYS = { days | @days_var }
指定必須經過多少天才可以重寫該備份媒體集。假如用變數 (@days_var) 指定,該變數必須為整型。
重要 假如 EXPIREDATE 或 RETAINDAYS 沒有指定,有效期將取決於 sp_configure 的 media retention 配置設置。這些選項僅僅阻止 SQL Server 重寫文件。用其它方法可擦除磁帶,而通過操作系統可以刪除磁碟文件。有關過期驗證的更多信息,請參見本主題的 SKIP 和 FORMAT。
PASSWORD = { password | @password_variable }
為備份集設置密碼。 PASSWORD 是一個字元串。如果為備份集定義了密碼,必須提供這個密碼才能對該備份集執行任何還原操作。
『叄』 用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"];
『肆』 vb.net中對sql server 資料庫中的數據進行備份的代碼……偶沒有錢了,拜託知道的大蝦幫幫忙,謝謝O(∩_∩)
寫個存儲過程! 執行存儲哦過程就行了!!把 備份的路徑當參數傳給存儲過程
CREATE PROCEDURE [dbo].[backstore] AS
declare @strback varchar(2000)
declare @bakstr varchar(128)
set @bakstr=convert(varchar,getdate(),12)+convert(varchar(10),datename(hh,getdate()))+convert(varchar(10),datename(mm,getdate()))+convert(varchar(10),datename(ss,getdate()))
set @strback='BACKUP DATABASE [eipnew] TO DISK = N''E:\hysoft\soft\pf\sqlpeobak\'+@bakstr+'eipnew.rar'' WITH NOINIT , NOUNLOAD , NAME = N''eipnew 備份'', NOSKIP , STATS = 10, NOFORMAT'
--select @strback
exec (@strback)
select @bakstr+'eipnew.rar'
GO
『伍』 怎麼用VB.NET 編碼實現備份SQL SERVER資料庫
具體可查一下 SQL命令
BACKUP DATABASE
RESTORE DATABASE
跟執行普通的SQL語句一樣調用就可以
『陸』 VB如何遠程備份sqlserver資料庫到本地機中
(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.file // & serverName & /SQLDATABACK2/ & Date & .dat, Trim(Text1), True 5.清理文件。
『柒』 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"];
『捌』 VB.NET 怎麼調用備份恢復SQL2008 資料庫求源碼。。
引用Microsoft SQLDMO Object Library
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SQLSER As New SQLDMO.SQLServer
SQLSER.Connect(ServerName, UserName, PassWord) '這三項換為你自己的
PBackup.Database = DatabaseName '資料庫名
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
Dim PathName As String
PathName = "D:\BackUp"
PBackup.Files = PathName
PBackup.SQLBackup(SQLSER)
SQLSER.DisConnect()
SQLSER = Nothing
Me.Cursor = System.Windows.Forms.Cursors.Default
MsgBox("成功備份了數據")
End Sub
『玖』 VB.NET 中資料庫使用數據備份和恢復的代碼!急急急!!!
備份資料庫SQL代碼:sql= "backup database 工廠管理 to disk=d:\DataBak.dat'還原資料庫SQL代碼sql="restore database 工廠管理 from disk=d:\DataBak.dat"
其它的你自己去寫了。