2

I'm trying to restore a database by using the following code

string Restore = @"RESTORE DATABASE [" + dbname+ "] FROM DISK = N'" + filePath + "' WITH REPLACE, FILE = 1,  NOUNLOAD,  STATS = 10";
SqlCommand RestoreCmd = new SqlCommand(Restore, con);
RestoreCmd.ExecuteNonQuery();

it returns me a timeout expired error, I have set connect timeout=300 in the connectionstring

but by running only the t-sql script RESTORE DATABASE [dbname] FROM DISK = N'filepath' WITH REPLACE, FILE = 1, NOUNLOAD, STATS = 10 it works fine

what should i do to fix this?

2
  • 1
    Not 100% sure as I have not done this for ages, but I believe there is a timeout on the SqlCommand too that you can specify
    – Ryk
    Feb 24, 2011 at 1:22
  • TYVM Ryk, I didn't know the solution was so simple, yes ,it worked! sqlcommand.commandtimeout is the time that a single command might take to run, while connect timeout setting in connectionstring is the time for establishing the connection.
    – walter
    Feb 24, 2011 at 1:36

1 Answer 1

3

Set the CommandTimeout property of you RestoreCmd like so:

RestoreCmd.CommandTimeout = 300;
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.