Skip to content

Instantly share code, notes, and snippets.

@jaysonrowe
Created September 18, 2012 01:20
Show Gist options
  • Select an option

  • Save jaysonrowe/3740722 to your computer and use it in GitHub Desktop.

Select an option

Save jaysonrowe/3740722 to your computer and use it in GitHub Desktop.
restoredb method
private static void restoredb(object source, RenamedEventArgs e)
{
Server svr = new Server(sqlserver);
Restore res = new Restore();
// only temporary until passing in site number as argument
string dbname = DateTime.Now.Millisecond.ToString();
res.Database = dbname;
res.Action = RestoreActionType.Database;
res.Devices.AddDevice(watcherpath + e.Name, DeviceType.File);
res.ReplaceDatabase = true;
DataTable dt;
DataRow[] foundrows;
dt = res.ReadFileList(svr);
foundrows = dt.Select();
string mdfname = foundrows[0]["LogicalName"].ToString();
string ldfname = foundrows[1]["LogicalName"].ToString();
res.RelocateFiles.Add(new RelocateFile(mdfname, dbpath + dbname + ".mdf"));
res.RelocateFiles.Add(new RelocateFile(ldfname, dbpath + dbname + "_log" + ".ldf"));
try
{
res.SqlRestore(svr);
}
catch (FailedOperationException fox)
{
str.WriteLine("{0} Exception caught.", fox.Message);
}
catch (Exception x)
{
str.WriteLine("{0} Exception caught.", x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment