Not able to Restore database from specified path in C# (SMO) - sqlserver-programming
This is a discussion on Not able to Restore database from specified path in C# (SMO) - sqlserver-programming ; trying to restore database as below private void button1_Click(object sender, EventArgs e) { StreamWriter writer = new StreamWriter(@"\servernamevnscript_output.txt", false); Server s = new Server("servername"); Database Database = s.Databases["ReportServer"]; // Set up the scripting options to be used below. ScriptingOptions so ...
![]() |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| |||
| |||
| private void button1_Click(object sender, EventArgs e) { StreamWriter writer = new StreamWriter(@"\servernamevnscript_output.txt", false); Server s = new Server("servername"); Database Database = s.Databases["ReportServer"]; // Set up the scripting options to be used below. ScriptingOptions so = new ScriptingOptions(); so.Permissions = true; so.Indexes = true; so.IncludeIfNotExists = true; // First script the database itself StringCollection coll = Database.Script(so); foreach (string str in coll) { writer.WriteLine(str); } //Restore Database Server srv = new Server("servername"); //Database Database = srv.Databases["Databasename"]; Restore res = new Restore(); res.Database = "ReportServer"; // Set the backup device from which we want to restore, to a file // BackupDeviceItem bkpDevice = new BackupDeviceItem(@"\ServernamevnReportServer.ba k", DeviceType.File); res.Devices.AddDevice(@"\ServernamevDatabasenam e.bak", DeviceType.File); res.Database = "ReportServer"; // res.Database = @"\servername1vn" + "ReportServer.mdf"; res.Action = RestoreActionType.Database; res.PercentCompleteNotification = 10; res.ReplaceDatabase = true; res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler); res.SqlRestore(srv); writer.Flush(); writer.Close(); } static void ProgressEventHandler(object sender, PercentCompleteEventArgs e) { Console.WriteLine(e.Percent.ToString() + "Restored"); } } } Getting error @line res.SqlRestore(srv); {"System.Data.SqlClient.SqlError: Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\ReportServer.mdf" failed with the operating system error 2(The system cannot find the file specified.)."} I guess its going to the defalut backup drive and searching instead of searching from defined path (@"\ServernamevDatabasename.bak", DeviceType.File) when i try to restore database from default drive it works fine , but i need to specify the path from where it has to restore Thanks |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| Display Modes | |
| |
All times are GMT -4. The time now is 02:04 AM.




Linear Mode