Cannot restore sql server 2005 database in c#, receiving Operating system error 2(The system cannot find the file specified.). error

Sep 11 2009 8:44 AM

I am developing an asp.net application (C#), in which i want to give user the functionality to backup an restore the MS-SQL Server database.
The back up code is working fine.
When i am trying to restore the taken back up, i am receiving an error as follows.
Cannot open backup device 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\myDb10-9-2009.bak'. Operating system error 2(The system cannot find the file specified.).
RESTORE DATABASE is terminating abnormally.
I am storing the .bak file in my E:\, but i am receiving an error of the default back up directory of MS-SQL Server 2005.
I have tried to restore the database with same .bak file through MS-SQL Server 2005, its working fine, but when i try through my code its giving the above error.
Following is my code.
 private void RestoreNew()
    {
        Server srv = default(Server);
        srv = new Server("ServerName");
        Database db = new Database(srv, "mydb"); //srv.Databases["mydb"];
        Restore rstDatabase = new Restore();
        rstDatabase.Database = db.ToString();
        BackupDeviceItem bkpDevice = new BackupDeviceItem(System.IO.Path.GetFileName(flRestore.PostedFile.FileName), DeviceType.File);
       
        rstDatabase.Devices.AddDevice(bkpDevice.Name, DeviceType.File);
        rstDatabase.Action = RestoreActionType.Database;
        rstDatabase.ReplaceDatabase = true;
        rstDatabase.ContinueAfterError = true;
       
        rstDatabase.SqlRestore(srv);
    }

Please help, i am going nuts.
Thanks,
Sandeep

Answers (2)