// add ref using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Smo.Wmi; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Smo.Agent; using Microsoft.SqlServer.Management.Smo.Broker; using Microsoft.SqlServer.Management.Smo.Mail; using Microsoft.SqlServer.Management.Smo.RegisteredServers; using Microsoft.SqlServer.Management.Trace; // Connection SqlConnection sq = new SqlConnection(ClassAccessDatac.con); // behind code btn private void btnBackup_Click(object sender, EventArgs e) { string MethodeName = "btnBackup_Click"; try { string strFileName = string.Empty; saveFileDialog1.DefaultExt = "mdf"; saveFileDialog1.FileName = "BackupFile" + un1.OnUniq(); saveFileDialog1.Filter = @"SQL Backup files *.*|All files(*.*) "; saveFileDialog1.FilterIndex = 1; saveFileDialog1.OverwritePrompt = true; saveFileDialog1.Title = "Backup SQL File"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { strFileName = saveFileDialog1.FileName; BackupDatabase("BozorgMehr", "sa", "09124863440", "SERVER001", strFileName); Backup(strFileName); } } catch (Exception ex) { Keys.RegError(MethodeName, ex.ToString(), sq); } } public Microsoft.SqlServer.Management.Smo.Database db = new Database(); public void BackupDatabase(String databaseName, String userName, String password, String serverName, String destinationPath) { try{ Backup sqlBackup = new Backup(); sqlBackup.Action = BackupActionType.Database; sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToShortDateString(); sqlBackup.BackupSetName = "Archive"; sqlBackup.Database = databaseName; BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File); ServerConnection connection = new ServerConnection(serverName, userName, password); Server sqlServer = new Server(connection); Database db = sqlServer.Databases[databaseName]; sqlBackup.Initialize = true; sqlBackup.Checksum = true; sqlBackup.ContinueAfterError = true; sqlBackup.Devices.Add(deviceItem); sqlBackup.Incremental = false; sqlBackup.ExpirationDate = DateTime.Now.AddDays(3); sqlBackup.LogTruncation = BackupTruncateLogType.Truncate; sqlBackup.FormatMedia = false; sqlBackup.SqlBackup(sqlServer); } catch (Exception ex) { Keys.RegError(MethodeName, ex.ToString(), sq); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)