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

发布于 2024-09-28 11:43:32 字数 224 浏览 2 评论 0 原文

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

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

我如何使用 C# 来做到这一点?

How can I use a .bak database backup file (backed up via a query in SQL Server) programmatically?

I want my application to back up my database to a location (which I can already do) and I also want it to be able to load a backed up database (the .bak file).

How can I do this using C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三五鸿雁 2024-10-05 11:43:32

您需要首先确保您的开发盒上安装了 SMO(SQL Server 管理对象)并可供您使用。如果您已在其上安装了某些版本的 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.SmoExtend 在名为 Microsoft.SqlServer.SmoExtended.dll 的程序集中实现,该程序集应在目录 < code>C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\(如果安装了 SMO)。

如果您没有安装 SMO,您可以从 此处 适用于 SQL Server 2008 R2(还有适用于 SQL Server 2005 的旧版本)

You need to first make sure you have the SMO (SQL Server Management Objects) installed and available to you on your dev box. This is typically the case, if you have installed some version of SQL Server on it.

If you have the SMO library available, you can use this code snippet for your operation:

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......
 }

For this to work, you need to have the following project references

alt text

and the namespace Microsoft.SqlServer.SmoExtended is implemented in the assembly called Microsoft.SqlServer.SmoExtended.dll which should be found in the directory C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\ if you have SMO installed.

If you don't have SMO installed, you can go fetch it from here for SQL Server 2008 or here for SQL Server 2008 R2 (there's also an older version for SQL Server 2005)

心的位置 2024-10-05 11:43:32

只需使用 SqlCommand.ExecuteNonQuery 来执行执行操作所需的 SQL,例如:

BACKUP DATABASE [dbname] ......

RESTORE DATABASE [dbname] ......

当然,相关 SQL 用户需要具有适当的权限。

Simply use SqlCommand.ExecuteNonQuery to execute the SQL needed to perform the operations, such as:

BACKUP DATABASE [dbname] ......

RESTORE DATABASE [dbname] ......

Of course the SQL user in question will need to have appropiate permissions.

蹲墙角沉默 2024-10-05 11:43:32

这是备份的方法:

-- =========================================================
-- 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' 

This is how to backup:

-- =========================================================
-- 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

And this is how to restore:

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' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文