sqlserver打開或創建mdf失敗

軟件中工程用到sqlserver數據庫存放數據,習慣性將數據放在桌面,但無法使用SQL Server Management Studio將數據庫附加進來。與此相同的問題還有如下幾種情形:

問題現象1

Msg 5133, Level 16, State 1, Line 1

Directory lookup for the file

"C:\Users\SQLTwins\Documents\AdventureWorks2014\AdventureWorks2014_Data.mdf"

failed with the operating system error 5(Access is denied.).

參考:https://nakulvachhrajani.com/2017/04/10/0405-sql-server-msg-5133-backuprestore-errors-directory-lookup-for-file-failed-operating-system-error-5access-is-denied/

 

問題現象2SQL Server Management Studio附加桌面路徑數據庫找不到所在文件夾

問題現象3在桌面無法使用代碼或者工具創建數據庫文件

問題現象4打開從其他地方拷貝的數據庫文件,提示權限不夠(非管理員權限)

本質:文件所在路徑的ACL權限不夠。修改ACL權限方法如下:

解決辦法:

方法一:修改目錄權限

在【編輯】裏增加【添加】everyone,確認,然後勾選【完全控制】、【修改】點擊【應用】即可。

注:此處添加everyone用戶組是爲了簡單,用戶也可以添加需要創建或修改數據庫文件的賬戶。

方法二:使用cacls或者icacls修改目錄權限

新建bat腳本,放入當前目錄執行即可

@echo off

icacls "%cd%" /grant Everyone:(OI)(CI)F /T

pause

或者

@echo off

cacls "%cd%"  /t /e /g everyone:f

pause

方法三:使用模擬啓動cmd方式(dotnet環境下可以使用)

 private static void ModifyProjectACL(string directory)

 {

            using (var myPro = new Process())

            {

                myPro.StartInfo.FileName = @"cmd.exe";

                myPro.StartInfo.UseShellExecute = false;

                myPro.StartInfo.RedirectStandardInput = true;

                myPro.StartInfo.RedirectStandardOutput = true;

                myPro.StartInfo.RedirectStandardError = true;

                myPro.StartInfo.CreateNoWindow = true;

               myPro.Start();

                myPro.StandardInput.WriteLine(directory.Substring(0, 2));

                myPro.StandardInput.WriteLine(@"cd " + directory);

                myPro.StandardInput.WriteLine(@"icacls " + "\"" + "%cd%" + "\"" + " /grant Everyone:(OI)(CI)F /T");

                myPro.StandardInput.AutoFlush = true;

            }

}

 

方法四:dotnet環境下

// Adds an ACL entry on the specified directory for the specified account.

public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)

        {

            // Create a new DirectoryInfo object.

            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the

            // current security settings.

            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.

            dSecurity.AddAccessRule(new FileSystemAccessRule(Account,  Rights,ControlType));

            // Set the new access settings.

            dInfo.SetAccessControl(dSecurity);

           // Set the sub directory's ACL

            foreach (var ditem in dInfo.GetDirectories())

            {

                AddDirectorySecurity(ditem.FullName, Account, Rights, ControlType);

            }

        }

調用:

AddDirectorySecurity(path,"Everyone",FileSystemRights.FullControl, AccessControlType.Allow);

參考:

[1] https://nakulvachhrajani.com/2017/04/10/0405-sql-server-msg-5133-backuprestore-errors-directory-lookup-for-file-failed-operating-system-error-5access-is-denied/

[2] https://docs.microsoft.com/zh-cn/dotnet/api/system.security.accesscontrol.directorysecurity?redirectedfrom=MSDN&view=netframework-4.8

[3] https://www.cnblogs.com/junior/archive/2012/03/31/2426355.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章