Не удалось подключиться к серверу. \ mydbserver. < /p>
< /p> < /> < /> < /> < />
Код: Выделить всё
private void DoDatabaseBackup(){
string message = "";
try
{
//SQLDMO.SQLServer server = new SQLServer();
Backup backup = new Backup();
var query = from e in XElement.Load("Databases.xml").Elements("BackupDatabase")
select new BackupDatabase
{
BackupPath = (string)e.Element("BackupPath"),
ServerName = (string)e.Element("ServerName"),
DBName = (string)e.Element("DBName"),
UserName = (string)e.Element("UserName"),
Password = (string)e.Element("Password")
};
List list = query.ToList();
foreach (BackupDatabase dbToBackup in list)
{
//BackupDatabase dbToBackup = list[0];
try
{
string backupPath = dbToBackup.BackupPath;
string backupFileName = string.Format("{0}_DB_{1}.bak", dbToBackup.DBName, dateTimePart);
string backupDestination = string.Format("{0}{1}", backupPath, backupFileName);
backup.Action = BackupActionType.Database;
backup.BackupSetDescription = string.Format("Backup of {0} on {1}", dbToBackup.DBName, dateTimePart);
backup.BackupSetName = "FullBackup";
backup.Database = dbToBackup.DBName;
BackupDeviceItem deviceItem = new BackupDeviceItem(backupDestination, DeviceType.File);
// define server connection
ServerConnection connection = new ServerConnection(@dbToBackup.ServerName, dbToBackup.UserName, dbToBackup.Password);
connection.LoginSecure = false;
Server sqlServer = new Server(connection);
sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
sqlServer.ConnectionContext.Connect();
using (EventLog elog = new EventLog("Application"))
{
message = "Connected successfully to " + dbToBackup.ServerName;
elog.Source = "Application";
elog.WriteEntry(message, EventLogEntryType.Information, 101, 1);
}
Database db = sqlServer.Databases[dbToBackup.DBName];
backup.Initialize = true;
backup.Checksum = true;
backup.ContinueAfterError = true;
backup.Devices.Add(deviceItem);
backup.Incremental = false; // set to be full database backup
backup.ExpirationDate = DateTime.Today.AddDays(180);
backup.LogTruncation = BackupTruncateLogType.Truncate; // log must be truncated after the backup is complete
backup.FormatMedia = false;
backup.SqlBackup(sqlServer);
backup.Devices.Remove(deviceItem);
string messageTitle = string.Format("{0} Backup Tool", dbToBackup.DBName);
message = string.Format("Backup has been taken successfully into the file: {0}{1}", backupPath, backupFileName);
//EventLog.WriteEntry(messageTitle, message, EventLogEntryType.Information);
using (EventLog elog = new EventLog("Application"))
{
elog.Source = "Application";
elog.WriteEntry(message, EventLogEntryType.Information, 101, 1);
}
}
catch (Exception ex)
{
using (EventLog elog = new EventLog("Application"))
{
message = string.Format("Error in Database Backup Tool For Database: {0} -- {1}", dbToBackup.DBName, ex.Message);
elog.Source = "Application";
elog.WriteEntry(message, EventLogEntryType.Warning, 101, 1);
}
//EventLog.WriteEntry("Error in Database Backup Tool For Database: " + dbToBackup.DBName, ex.Message, EventLogEntryType.Warning);
continue;
}
}
}
catch (Exception ex)
{
using (EventLog elog = new EventLog("Application"))
{
message = string.Format("Error in Database Backup Tool {0}", ex.Message);
elog.Source = "Application";
elog.WriteEntry(message, EventLogEntryType.Warning, 101, 1);
}
}
< /code>
} < /p>
И это содержимое баз данных.
D:\backups\
.\MyDbServer
QassimDarb
test
test
Подробнее здесь: https://stackoverflow.com/questions/796 ... stallation