Showing posts from October, 2011

Restoring a database using C#

This code works perfectly and can be configured in any manner:
I was struggling due to command timeout and WITH RECOVERY options not present in my code earlier...... Please note that if your database has differential files, set the tsql to NORECOVERY:

string dataFileName = string.Empty;
string logFileName = string.Empty;
string getRestoreQueryList = @" RESTORE FILELISTONLY FROM DISK = N'C:\Test\Ishwar.bak'";
SqlCommand getRestoreQueryListCommand = new SqlCommand(getRestoreQueryList, con);
SqlDataAdapter adp = new SqlDataAdapter(getRestoreQueryList, con);
DataSet ds = new DataSet();
foreach (DataRow dr in ds.Tables[0].Rows)
if ("PRIMARY" == dr["FileGroupName"].ToString())
dataFileName = dr["LogicalName"].ToString();