Hey! I was developing an application that perform replication programmatic from local client machine to main server. there i feel need of backup and restore database. So below is the code for taking backup automatically
public Boolean BackUp(string ServerName, string Login, string Password) { try { SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass(); //connect to the server srv.Connect(ServerName, Login, Password); //create a backup class instance SQLDMO.Backup bak = new SQLDMO.BackupClass(); //set the backup device = files property ( easy way ) bak.Devices = bak.Files; //set the files property to the File Name text box bak.Files = "c:\\Fides-Backup.bak"; //set the database to the chosen database bak.Database = "ReplicTest"; bak.Initialize = true; //OverWritten existing database //perform the backup bak.SQLBackup(srv); //MessageBox.Show("Database successfully backed up.", "Backup Successfull"); return true; } catch (Exception err) { return false; //MessageBox.Show(err.Message, "Error"); } }
ReplicTest is my database name. so you have to change your database name c:\\Fides-Backup.bak is path where backup will save
and don't forget to add reference of SQLDMO in your project, feel free to ask
|
No responses found. Be the first to respond and make money from revenue sharing program.
|