0

I have a mssql database on remote machine and backup file for this database. I want to create a method (c#) for restoring of database from backup. I will execute my method on my local machine. Can somebody help me to create such method for restoring remote database?

1
  • Where do you need to restore the DB?Showing us some code of what you've tried would also be helpful. Commented Aug 15, 2014 at 8:45

1 Answer 1

1

Try the this:

    public  void RestoreDatabase(string fileName)
    {
        try 
        {           
            using (SqlConnection conn = new SqlConnection("connectionString"))
            {
                string sql = "RESTORE DATABASE YourDatabase FROM DISK = N''" + fileName;
                conn.Open();
                SqlCommand _command = new SqlCommand(sql, conn);
                _command.ExecuteNonQuery();                
            }
        }
        catch (Exception ex)
        {
             throw;
        }
    }

You call it this way :

    RestoreDatabase(@"\\remotemachine\...\YourFile.bak");

NB: Put an actual path for where the file is located

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.