Created
September 18, 2012 01:20
-
-
Save jaysonrowe/3740722 to your computer and use it in GitHub Desktop.
restoredb method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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