这是我备份我跟随this tutorial的数据库的代码,正如您所看到的,代码是相同的。填写变量后出现错误。我得到的错误是
服务器'RITZEL-PC \ SQLEXPRESS'的备份失败。
备份功能:
public void BackupDatabase(String databaseName, String userName, String password, String serverName, String destinationPath)
{
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);
}
点击按钮
private void BackUp_Btn_Click(object sender, EventArgs e)
{
String databaseName = @"D:\MY_THESIS\WORKING FILES\NNIT-RMS.mdf";
String userName = "NNIT-Admin";
String password = "password";
String serverName = @"RITZEL-PC\SQLEXPRESS";
String destinationPath = @"D:\";
BackupDatabase(databaseName,userName,password,serverName,destinationPath);
}
数据库信息 我不允许发布图片,但我上传了一个屏幕截图。见这里:http://img268.imageshack.us/img268/9250/sqlg.jpg
答案 0 :(得分:1)
为了备份数据库,SQL Server正在运行,因为需要对备份所针对的文件夹具有正确的文件夹权限。
根据我的经验,除非SQL服务器进程作为本地管理员运行,否则它将无权写入根目录。
如果您打开SSMS并尝试将数据库备份到目标路径,则可能会遇到同样的问题。
尝试将备份文件夹更改为驱动器根目录或我的文档文件夹以外的其他文件。
答案 1 :(得分:0)
再次分配字符串值你只需删除它们并按如下方式调用备份:
BackupSqlDatabase("YourDataBaseName","sa","password","ServerName","C:\\YourDataBaseName.bak");