代码之家  ›  专栏  ›  技术社区  ›  Smiley

以编程方式使用备份数据库

  •  8
  • Smiley  · 技术社区  · 14 年前

    如何使用.bak数据库备份文件(通过中的查询进行备份) SQL Server )以编程方式?

    我希望我的应用程序将我的数据库备份到一个位置(我已经可以这样做了),我还希望它能够加载备份的数据库(.bak文件)。

    我怎样才能用C来做这个?

    4 回复  |  直到 9 年前
        1
  •  15
  •   Cyril Gandon niktrs    14 年前

    您需要首先确保在您的dev框中安装了SMO(SQL Server管理对象),并且对您可用。如果您在其上安装了某些版本的SQL Server,则通常会出现这种情况。

    如果您有可用的SMO库,可以使用此代码段进行操作:

    使用microsoft.sqlserver.management.common; 使用microsoft.sqlserver.management.smo; 静态void main(string[]args) { //创建SMO服务器对象的实例 server myserver=new server(“(local)”); //创建“restore”对象的新实例 restore res=new restore(); res.database=“smo”;//您的数据库名称 //定义选项 res.action=restoreAreactionType.database; res.devices.adddevice(@“c:\smotest.bak”,deviceType.file); Res.Percent完成百分比=10; res.replacedatabase=真; //定义一个回调方法来显示进度 Res.PercentComplete+=新的PercentCompleteEventHandler(Res_PercentComplete); //执行还原 res.sqlrestore(myserver); } //显示还原进度的方法 静态void res_percentcomplete(对象发送程序,percentcompleteventargs e) { //做点什么…… } < /代码>

    要使其工作,需要具有以下项目引用

    以及命名空间 microsoft.sql server.smoextended is implemented in the assembly called microsoft.sql server.smoextended.dll which should be found in the directory c:\program files\microsoft SQL Server\100\sdk\assembles\ if you have smo installed.

    如果没有安装SMO,可以从 here for SQL Server 2008 or 此处 for SQL Server 2008 R2(还有一个较旧版本用于SQL Server 2005)

    管理对象)。如果您在其上安装了某种版本的SQL Server,则通常是这种情况。

    如果您有可用的SMO库,可以使用此代码段进行操作:

    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    
    static void Main(string[] args)
    {
        // create instance of SMO Server object
        Server myServer = new Server("(local)");
    
        // create new instance of "Restore" object    
        Restore res = new Restore();
        res.Database = "SMO";  // your database name
    
        // define options       
        res.Action = RestoreActionType.Database;
        res.Devices.AddDevice(@"C:\SMOTest.bak", DeviceType.File);
        res.PercentCompleteNotification = 10;
        res.ReplaceDatabase = true;
    
        // define a callback method to show progress
        res.PercentComplete += new PercentCompleteEventHandler(res_PercentComplete);
    
        // execute the restore    
        res.SqlRestore(myServer);
     }
    
     // method to show restore progress
     static void res_PercentComplete(object sender, PercentCompleteEventArgs e)
     {
        // do something......
     }
    

    要使其工作,您需要具有以下项目引用

    alt text

    和命名空间 Microsoft.SqlServer.SmoExtended 在调用的程序集中实现 Microsoft.SqlServer.SmoExtended.dll 应该在目录中找到 C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\ 如果安装了SMO。

    如果没有安装SMO,可以从 here 对于SQL Server 2008或 here 对于SQL Server 2008 R2(还有一个旧版本的SQL Server 2005)

        2
  •  5
  •   driis    14 年前

    只需使用sqlcommand.executenonquery执行执行操作所需的SQL,例如:

    BACKUP DATABASE [dbname] ......
    
    RESTORE DATABASE [dbname] ......
    

    当然,有问题的SQL用户需要具有适当的权限。

        3
  •  1
  •   Stefan Steiger Marco van de Voort    14 年前

    以下是备份方法:

    -- =========================================================
    -- Author:        Stefan
    -- Create date:   16.07.2010
    -- Last mutation: 16.07.2010
    -- Description:   Backup der ausgewählten Datenbank
    -- =========================================================
    CREATE PROCEDURE [dbo].[sp_BackupDatabase] 
        @in_strDataBase varchar(50)
        --,@in_strUser varchar(36)
    
    AS
    BEGIN
    
    DECLARE @strBasePath  nvarchar(3000)
    DECLARE @strFileName  nvarchar(1000)
    
    DECLARE @strFileNameAndPath  nvarchar(4000)
    
    SET @strBasePath = 'E:\Temp\'
    
    SET @strFileName = @in_strDataBase
    SET @strFileName = @strFileName + '_'
    SET @strFileName = @strFileName + convert(varchar, getdate(), 112)
    SET @strFileName = @strFileName + '_' + REPLACE(convert(varchar, getdate(), 108),':','_'); 
    SET @strFileName = @strFileName + '_sts' 
    SET @strFileName = @strFileName + '.bak'
    
    SET @strFileNameAndPath = @strBasePath + @strFileName
    
    PRINT @strFileNameAndPath
    
    BACKUP DATABASE @in_strDataBase TO DISK=@strFileNameAndPath
    
    END
    
    GO
    

    这就是如何恢复:

    RESTORE DATABASE MyDatabase
    FROM DISK='C:\temp\MyDatabase_20100810.bak' 
    WITH REPLACE,
    MOVE 'MyDatabase' TO 'E:\SQLData_2008\MyDatabase.mdf',
    MOVE 'MyDatabase_log' TO 'E:\SQLData_2008\MyDatabase.ldf'