SQL Server数据库任务,分离,备份和还原

时间:2012-02-12 07:04:28

标签: sql-server vb.net

我通过vb.net代码附加了我的数据库。

现在我请求以下方面的帮助:

  1. 如何使用代码(uid=sa, pwd=abc123)分离它?
  2. 如何备份?
  3. 如何恢复备份?
  4. 请注意我想使用vb.net代码

    完成所有操作

    非常感谢。

2 个答案:

答案 0 :(得分:2)

本系列文章是一个很好的入门和查看示例的地方 - 也在VB.NET中。

Getting started with SMO in SQL Server 2005

即使您遇到C#样本 - 也总是有在线Convert C# to VB.NET网站将这些转换为VB.NET

答案 1 :(得分:0)

备份:

USE AdventureWorks2008R2;
GO
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2.Bak'
   WITH FORMAT,
      MEDIANAME = 'Z_SQLServerBackups',
      NAME = 'Full Backup of AdventureWorks2008R2';
GO

还原:

----Put database into single user mode (terminates open connections - else restore fails)
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE


RESTORE DATABASE YourDB 
FROM DISK = 'D:\temp\YourDB.bak' 
WITH REPLACE 
,MOVE 'YourDB_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.mdf'
,MOVE 'YourDB_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.ldf'


ALTER DATABASE YourDB SET MULTI_USER
GO

分离:

EXEC master.dbo.sp_detach_db @dbname = N'AdventureWorks',
@keepfulltextindexfile = N'true'
GO

附加:

EXEC master.dbo.sp_attach_db @dbname = N'AdventureWorks2008R2', 
    @filename1 = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\AdventureWorks2008R2_Data.mdf', 
    @filename2 = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\AdventureWorks2008R2_log.ldf';