hi guys...Im using SQLDMO to backup my database ('MyDB') and it works smoothly but when I delete the backed-up database('MyDB') from the server and try to restore it from the backup file using SQLDMO it has an error something like
Isn't posible for SQLDMO to just restore the database even though the Database that im restoring does not exist in the database? any help guys..Thanks in advance! oh! by the way my code..[[Microsoft][ODBS SQL Server Driver][SQL Server]Database 'MyDB' does not exist.
Backup Database:
Restore Database:Code:public static bool BackUpDB(string serverName, string DbName, string userName, string passWord, string pathFileName, bool winAuthentication, RCPSIndex parentForm) { bool returnVal = false; try { if (File.Exists(pathFileName)) { File.Delete(pathFileName); } pathFileName = @"[" + pathFileName + "]"; SQLDMO._SQLServer sqlSrv = new SQLDMO.SQLServerClass(); if (winAuthentication) { sqlSrv.LoginSecure = true; sqlSrv.Connect(serverName, null, null); } else { sqlSrv.Connect(serverName, userName, passWord); } SQLDMO.Backup sqlBakup = new SQLDMO.BackupClass(); sqlBakup.Devices = sqlBakup.Files; sqlBakup.Files = pathFileName; sqlBakup.Database = DbName; sqlBakup.SQLBackup(sqlSrv); //Set Last Backup date from RCPS Settings RCPSSettingsDataBase DBSettings = parentForm.SettingsDB; DBSettings.dbBackRecoLastBackupDate = DateTime.Now; DBSettings.Save(); parentForm.FuncInitializeDBSettings(); returnVal = true; } catch (Exception ex) { MessageBox.Show("Backup Error!\r\nMessage:" + ex.Message, "RCPS [Backup]", MessageBoxButtons.OK, MessageBoxIcon.Error); } return returnVal; }
Code:public static bool RestoreDB(string serverName, string DbName, string userName, string passWord, string fileName, bool winAuthentication) { bool returnVal = false; try { fileName = "[" + fileName + "]"; SQLDMO._SQLServer SqlSvr = new SQLDMO.SQLServerClass(); if (winAuthentication) { SqlSvr.LoginSecure = true; SqlSvr.Connect(serverName, null, null); } else { SqlSvr.Connect(serverName, userName, passWord); } SqlSvr.KillDatabase(DbName); SQLDMO.Restore DBRestore = new SQLDMO.RestoreClass(); DBRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; DBRestore.Database = DbName; DBRestore.Devices = DBRestore.Files; DBRestore.Files = fileName; DBRestore.ReplaceDatabase = true; DBRestore.SQLRestore(SqlSvr); returnVal = true; } catch (Exception ex) { MessageBox.Show("Restore Error!\r\nMessage:" + ex.Message, "RCPS [Restore]", MessageBoxButtons.OK, MessageBoxIcon.Error); } return returnVal; }




Reply With Quote