2011-03-13 39 views

回答

0

我用下面的代碼來備份SQL Server數據庫:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Collections; 
using Microsoft.SqlServer.Management.Common; 
using Microsoft.SqlServer.Management.Smo; 
using System.Text; 

namespace Codeworks.SqlServer.BackupDatabase 
{ 
    public class BackupCore 
    { 
     public static void Execute(string instance, string database, string outputFile) 
     { 
      BackupDeviceItem bdi = new BackupDeviceItem(outputFile, DeviceType.File); 
      Backup bu = new Backup(); 
      bu.Database = database; 
      bu.Devices.Add(bdi); 
      bu.Initialize = true; 

      // add percent complete and complete event handlers 
      bu.PercentComplete += new PercentCompleteEventHandler(Backup_PercentComplete); 
      bu.Complete +=new ServerMessageEventHandler(Backup_Complete); 

      Server server = new Server(instance); 
      bu.SqlBackup(server); 
     } 

     protected static void Backup_PercentComplete(object sender, PercentCompleteEventArgs e) 
     { 
      // Console.WriteLine(e.Percent + "% processed."); 
     } 

     protected static void Backup_Complete(object sender, ServerMessageEventArgs e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 
} 

你需要從MS管理庫正確的SQL Server版本,但這些都是可供下載。

+0

可能與您提到Access無關。我可以建議切換到一個合適的數據庫,而不是一些Office噱頭來存儲重要數據嗎? ;) – 2011-03-13 14:21:41

+0

感謝您的回覆!欣賞它! :D – Donald 2011-03-13 14:56:13

+0

數據庫是Jet或ACE,而不是MS Access,MS Access不是數據庫。你建議什麼其他數據庫是免費的,並默認安裝在Windows? – Fionnuala 2011-03-13 16:06:50

2

MS Access是基於文件的數據庫,對吧?在我的理解中,這意味着,當連接關閉並且文件未被使用時,可以將該文件複製到另一個位置。

這裏我假設應用程序在文件系統上擁有這些權限。

而且,我同意莫滕馬特內的答案,如果數據庫類型是MS SQL服務器,那麼你一定會需要SMO庫使用。

+0

嗯..其中MS SQL服務器?..它也適用於MS訪問? – Donald 2011-03-13 15:02:00

+0

這適用於任何基於文件的數據庫,只要應用程序可以將該文件從一個位置複製到另一個位置即可。 – Tengiz 2011-03-14 07:22:41

0

如果你是你的數據庫的一個用戶,你只需要關閉連接和文件系統複製。

如果有多個用戶,那麼你應該使用不同的方法。如果你確實有可用的訪問,還有一個未公開的命令,這將使該表的備份一個Jet/ACE文件:

Application.SaveAsText 6, vbNullString, strTargetMDB 

現在,因爲這隻能與數據庫的訪問UI開放來完成,它需要自動化Access並在CurrentDB上運行。下面是訪問中運行的實現:

Public Function CreateBackup(strMDBName As String, strBackupPath As String, _ 
    Optional bolCompact As Boolean = False) As Boolean 
    On Error GoTo errHandler 
    Dim objAccess As Object 
    Dim strBackupMDB As String 
    Dim strCompactMDB As String 

    If Len(Dir(strBackupPath & "\*.*")) = 0 Then ' alternative: use File System Object for this 
     MkDir strBackupPath 
    End If 

    Set objAccess = New Access.Application 
    objAccess.Application.OpenCurrentDatabase strMDBName 
    strBackupMDB = "Backup" & Format(Now(), "YYYYMMDDhhnnss") & ".mdb" 
    Debug.Print strBackupPath & "\" & strBackupMDB 
    objAccess.Application.SaveAsText 6, vbNullString, strBackupPath & "\" & strBackupMDB 
    objAccess.Application.Quit 
    Set objAccess = Nothing 

    If bolCompact Then 
     strCompactMDB = strBackupPath & "\" & "c_" & strBackupMDB 
     Name strBackupPath & "\" & strBackupMDB As strCompactMDB 
     DBEngine.CompactDatabase strCompactMDB, strBackupPath & "\" & strBackupMDB 
     Kill strCompactMDB 
    End If 

    CreateBackup = (Len(Dir(strBackupPath & "\" & strBackupMDB)) > 0) 

    exitRoutine: 
    If Not (objAccess Is Nothing) Then 
     On Error Resume Next 
     objAccess.Application.Quit 
     On Error GoTo 0 
     Set objAccess = Nothing 
    End If 
    Exit Function 

    errHandler: 
    Select Case Err.Number 
     Case 75 ' Path/File access error -- tried to MkDir a folder that already exists 
     Resume Next 
     Case Else 
     MsgBox Err.Number & ": " & Err.Description, vbExclamation, "Error in CreateBackup()" 
     Resume exitRoutine 
    End Select 
    End Function 

要運行在C#中,你不得不自動訪問,你可能不希望在訪問的依賴。

由於我在訪問專門的工作,這是我使用的方法,所以我從來沒有編程的更復雜的方法。

如果您對數據庫有獨佔訪問,你可以使用JRO CompactDatabase命令緊湊一個新的文件名,但如果你有獨佔訪問,您還可以使用文件系統。

因此,基本上,您可以選擇如何將數據表導出到備份數據庫。您可以使用DoCmd.TransferDatabase複製所有數據表,然後複製這些關係,或者可以創建一個空模板數據庫,並將每個表中的數據依次追加到模板的副本中(順序不會當然違反RI)。

無論這些聽起來一點也不凌亂對我來說,這就是爲什麼我用SaveAsText方法!但是如果我沒有運行Access,那麼另外兩種方法是值得的。