使用C#进行数据库备份和还原

问题描述:

HI
我已下载此代码

使用C#和.NET 2.0的SQL Server 2005数据库备份和还原 [

HI
I was download this code

SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[^]

when I use Restore code in my project can''t restore (the message:restore to database ... fail)
but backup code is ok.
all of refrence are completely;

what dio I do? whould you please help me?

请确保在还原之前已关闭所有与数据库的连接.
除非关闭所有现有连接,否则将无法进行恢复.
Make sure all connections to the database are closed before doing a restore.
A restore wont happen unless all existing connections are closed.


立即找出问题根源的解决方案是,将try .. catch block引入:

Immediate solution to identify the root cause for this problem, would be to introduce try.. catch block as:

public void RestoreDatabase(String databaseName, String filePath,
       String serverName, String userName, String password,
       String dataFilePath, String logFilePath)
{
  try
  {
    Restore sqlRestore = new Restore();
    ......
  }
  catch (Exception ex)
  {
    Console.WriteLine("Restore error is: " + ex.Message);
  }
}



通常,该错误可能是由于对Transact-SQL上的RESTORE的SQL特定限制很少,如下所示:

SQL Server中的还原方案是从一个或多个备份还原数据然后恢复数据库的过程.支持的还原方案取决于数据库的恢复模型和SQL Server的版本.

*要设置还原对象属性,用户必须在服务器上具有CREATE DATABASE权限,或者是sysadmin或dbcreator固定服务器角色的成员,或者是db_owner固定数据库角色的成员.

*仅在SQL Server 2005 Enterprise Edition和更高版本中允许在线还原.



Mostly, the error might be because of few SQL specific restriction for RESTORE on Transact-SQL, as below:

A restore scenario in SQL Server is the process of restoring data from one or more backups and then recovering the database. The supported restore scenarios depend on the recovery model of the database and the edition of SQL Server.

* To set Restore object properties, users must have CREATE DATABASE permission on the server, or be a member of sysadmin or dbcreator fixed server roles, or be a member of db_owner fixed database role.

* Online restore is allowed only in SQL Server 2005 Enterprise Edition and later versions.