Reputation:
I am currently using the code below. I had to increase the timeout in order to stop the disconnect. The database is only 1gb and I'm not sure why this database never backs up. Any clues? No error is reported it just doesn't backup
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
public static void BackupDatabase(string backUpFile)
{
string conString = string.Format("Server=server;initial catalog=master;Integrated Security=SSPI;");
ServerConnection con = new ServerConnection(conString);
con.ConnectTimeout = 70000;
con.StatementTimeout = 70000;
Server server = new Server(con);
Backup source = new Backup();
source.Action = BackupActionType.Database;
source.Database = "backedupdb";
BackupDeviceItem destination = new BackupDeviceItem(backUpFile+"\\"+source.Database.ToString(), DeviceType.File);
source.Devices.Add(destination);
source.Initialize = false;
source.PercentComplete += CompletionStatusInPercent;
source.Complete += Backup_Completed;
source.SqlBackup(server);
con.Disconnect();
}
private static void CompletionStatusInPercent(object sender, PercentCompleteEventArgs args)
{
Console.Clear();
Console.WriteLine("Percent completed: {0}%.", args.Percent);
}
private static void Backup_Completed(object sender, ServerMessageEventArgs args)
{
Console.WriteLine("Hurray...Backup completed.");
Console.WriteLine(args.Error.Message);
}
private static void Restore_Completed(object sender, ServerMessageEventArgs args)
{
Console.WriteLine("Hurray...Restore completed.");
Console.WriteLine(args.Error.Message);
}
Upvotes: 0
Views: 549
Reputation:
The connection string is the issue in the above example, it does not want a "connectionstring" it simply wants the server name. The connection does not have to be initialized it will automatically be called.
string conString = string.Format("server");
ServerConnection con = new ServerConnection(conString);
Upvotes: 1