我正在尝试在我的WPF应用程序中使用C#来备份选定的数据库。我认为代码没问题,但是当备份运行时,我会得到这个错误:
自从我收到了那个错误,我尝试通过C#将文件夹权限授予所有人,但我仍然遇到同样的问题。非常感谢任何帮助。先感谢您。
这是我的代码:
这是我的代码:
DirectorySecurity sec = Directory.GetAccessControl(backupFolder);
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(backupFolder, sec);
List<string> dbNameList = GetDatabaseList();
if (dbNameList != null)
{
SqlCommand oCommand = null;
SqlConnection oConnection = null;
foreach (string dbName in dbNameList)
{
string command = @"BACKUP DATABASE " + dbName + " TO DISK='" + backupFolder + "'";
oConnection = new SqlConnection(ConnectionString);
if (oConnection.State != ConnectionState.Open)
oConnection.Open();
oCommand = new SqlCommand(command, oConnection);
oCommand.ExecuteNonQuery();
}
oConnection.Close();
}