Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
con.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Database1;Integrated Security=True;User Instance=True";
         con.Open();
      cmd.CommandText = @"restore database Database1  from disk ='d:\\MyDatabaseBackup.bak'";

         cmd.Connection = con;
         cmd.ExecuteNonQuery();
         cmd.Parameters.Clear();
         con.Close();
         MessageBox.Show("Restore COMPLETE");

This code has no error. It Pop up Restore complete.

1)I want to extract mdf file from the backup file at specified location.(How can i specify the destination location in sql command )
2)above code runing properly means no error, but i don't know where the database is restored.

and i notice one thing here that, it's only successfull(means message box show Restore complete) to restore the database if the project from which backup my database is on it's same location
Posted
Updated 3-Oct-12 3:26am
v2
Comments
footballpardeep 3-Oct-12 9:18am    
Unable to open the physical file "c:\users\pardeep\documents\visual studio 2010\Projects\Bhoot\Bhoot\BhootDatabase.mdf". Operating system error 3: "3(The system cannot find the path specified.)".
RESTORE DATABASE is terminating abnormally.
footballpardeep 3-Oct-12 9:19am    
Now i am getting this exception
footballpardeep 3-Oct-12 9:21am    
c:\users\pardeep\documents\visual studio 2010\Projects\Bhoot\Bhoot\BhootDatabase.mdf"

This was the path of my database when i make it's backup, if the project is on the same locatio then it say , successfull, but when i change the location of project, it gives my above error.

1 solution

You can use the T-SQL Restore statement like shown below
SQL
Restore DATABASE databaseName
FROM DISK='backup file path'
WITH NORECOVERY,
MOVE 'DataFile logical name' TO 'FileSystemPath',
MOVE 'LogFile logical name'  TO 'FileSystemPath'
 
Share this answer
 
Comments
footballpardeep 3-Oct-12 10:39am    
con.ConnectionString = @"Data Source=.\SQLEXPRESS;Integrated Security=True;Initial Catalog=BRdatabase;User Instance=True";

con.Open();
cmd.CommandText = @"use [master];restore database BRdatabase from disk ='d:\\BRdatabaseBackup.bak' WITH NORECOVERY, MOVE 'BRdatabase' TO '" + Directory.GetCurrentDirectory().ToString() + "', MOVE 'BRdatabase_log' TO '" + Directory.GetCurrentDirectory().ToString() + "'";


cmd.Connection = con;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
MessageBox.Show("rESORE COMPLETE");
footballpardeep 3-Oct-12 10:41am    
above code give exception:
System.Data.SqlClient.SqlException (0x80131904): File 'F:\BRsetup' is claimed by 'BRdatabase_log'(2) and 'BRdatabase'(1). The WITH MOVE clause can be used to relocate one or more files.
RESTORE DATABASE is terminating abnormally.
Changed database context to 'master'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900