【发布时间】:2014-09-07 19:28:37
【问题描述】:
我的问题是我无法使用 C# 代码在 wpf 中备份我的数据库。错误是“服务器 'DELL/LOCALSERVER' 的备份失败”。我使用的代码是这样的:
private void btnBackup_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtPath.Text) && !string.IsNullOrWhiteSpace(txtPath.Text)
&& !string.IsNullOrEmpty(txtFileName.Text) && !string.IsNullOrWhiteSpace(txtFileName.Text))
{
BackupFilePath = txtPath.Text + "" + txtFileName.Text + ".bak";
Server sqlServerInstance = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection
(new System.Data.SqlClient.SqlConnection(h2prdb.ConnectionString)));
Backup objBackup = new Backup();
objBackup.Database = "H2RPDB";
objBackup.Action = BackupActionType.Database;
objBackup.Devices.AddDevice(@BackupFilePath, DeviceType.File);
objBackup.SqlBackup(sqlServerInstance);
System.Windows.Forms.MessageBox.Show("The backup of database " + "'H2RPDB'" + " completed sccessfully", "Microsoft SQL Server Management Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
System.Windows.Forms.MessageBox.Show("Please fill-up all fields!",
"Backup/Restore", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
我不知道对这段代码不熟悉有什么问题。感谢您的回答。
【问题讨论】: