我正在尝试通过c#代码将本地SQL Server数据库备份到本地计算机,并且我的服务器备份失败。
代码如下:
using (var con = new SqlConnection(conn))
{
const string destination = "d:\\backups"+"/";
var sqlServer = new Server(new ServerConnection(con));
con.Open();
var bkpDatabase = new Backup { Action = BackupActionType.Database, Database = name };
var bkpDevice = new BackupDeviceItem(destination + name + ".bak", DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.Checksum = true;
bkpDatabase.ContinueAfterError = true;
bkpDatabase.Incremental = false;
bkpDatabase.Initialize = true;
// Perform the backup
bkpDatabase.SqlBackup(sqlServer);
con.Close();
}
我使用了以下参考资料:
mircrosoft.sqlserver.smo;
mircrosoft.sqlserver.smoextended;
mircrosoft.sqlserver.management.sdk.sfc;
mircrosoft.sqlserver.connectioninfo;
内部异常:
Microsoft.SqlServer.Management.Smo.FailedOperationException was caught
HelpLink=http://go.microsoft.com/fwlink? ProdName=Microsoft+SQL+Server&ProdVer=10.50.2500.0+((KJ_PCU_Main).110617- 0026+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptio nText&EvtID=Backup+Server&LinkId=20476
HResult=-2146233088
Message=Backup failed for Server 'XXXXX'.
Source=Microsoft.SqlServer.SmoExtended
Operation=Backup
StackTrace:
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
at DatabaseCreation.Program.BackupDatabase(String name, String conn) in c:\Users\Documents\Visual Studio 2013\Projects\Databaseinstallation\DatabaseCreation\Program.cs:line 154
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.SqlServer.ConnectionInfo
StackTrace:
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_IsOpen()
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.PoolConnect()
at Microsoft.SqlServer.Management.Common.ConnectionManager.get_DatabaseEngineType()
at Microsoft.SqlServer.Management.Smo.Server.GetExecutionManager()
at Microsoft.SqlServer.Management.Smo.Server.get_ExecutionManager()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_ServerVersion()
at Microsoft.SqlServer.Management.Smo.Backup.Script(Server targetServer)
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
答案 0 :(得分:2)
也许你必须逃避目标路径中的\
字符:
@"d:\backups"
或
"d:\\backups"
尝试以下方法:
using (var con = new SqlConnection(conn))
{
const string destination = "c:\\backups";
var fileName = Path.Combine(destination, String.Format("{0}.bak", name));
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
var sqlServer = new Server(new ServerConnection(con));
var bkpDatabase = new Backup
{
Action = BackupActionType.Database,
Database = name
};
var bkpDevice = new BackupDeviceItem(fileName, DeviceType.File);
bkpDatabase.Devices.Add(bkpDevice);
bkpDatabase.Checksum = true;
bkpDatabase.ContinueAfterError = true;
bkpDatabase.Incremental = false;
bkpDatabase.Initialize = true;
bkpDatabase.SqlBackup(sqlServer);
}