There are many ways to do this:
1. Using SMO (Management Objects)
using Microsoft.SqlServer.Management.Smo;
Server svr = new Server();
Backup bkp = new Backup();
bkp.Devices.AddDevice(@"C:\YourDatabase.bak", DeviceType.File);
bkp.Database = "YourDATABASE";
bkp.Action = BackupActionType.Database;
bkp.Initialize = true;
bkp.SqlBackup(svr);
This is the easiest way. But you need the reference to SMO objects.
2. Use Backup script to do this :
string sql = "BACKUP DATABASE [YourDatabase] TO DISK = N'C:\YourDatabase.bak' WITH NOFORMAT, NOINIT, NAME = N'YourDatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
using(SqlConnection con = new SqlConnection(masterconstring))
{
SqlCommand cmd = new SqlCommand(sql);
cmd.ExecuteNonQuery();
}
In this case you can connect normally to the master database and run the backup script.
:thumbsup: