If you are attaching a SQL database and read-only or fail, let's look at the solution in this article.
If you are attaching a SQL database and read-only or fail, let's look at the solution in this article.
The key points to solve this problem are as follows:
1. This phenomenon only appears in the NTFS file system.
2. Access Permissions Of the NTFS file system.
The solution for manual operations is as follows:
1. Control Panel-> Folder Options-> View and close "use simple file sharing (recommended )"
2. Select a folder for storing database files and right-click the folder and select Properties. A "security" tab appears. Select the last user and set the user's permissions to "full control ".
After these two steps, the database attaching failure or the attached read-only problem can be solved.
Next we will focus on the two steps and use the C # code to implement them, because when we pack the project, it is impossible for users to manually execute these operations.
1. C # disable code implementation for simple file sharing
When Baidu and Google search for the solution to this problem, many operations are incorrect. After actual test, the correct method is as follows:
File:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Lsa]
"Forceguest" = dword: 00000000
:
Reg add HKLM \ SYSTEM \ CurrentControlSet \ Control \ Lsa/v forceguest/t REG_WORD/d 0x00000000/f
If this option is selected, the preceding 0x00000000 is changed to 0x00000001.
The Code is as follows:
Microsoft. Win32.RegistryKey = Microsoft. Win32.Registry. LocalMachine;
Microsoft. Win32.RegistryKey software = key. OpenSubKey (@ "SYSTEM \ CurrentControlSet \ Control \ Lsa", true );
Software. SetValue ("forceguest", 0 );
2. C # modify the user access permission code of the specified folder as follows: (set the user's permission to writable)
String path = this. Context. Parameters ["targetdir"]; // obtain the folder path.
DirectoryInfo dInfo = new DirectoryInfo (path );
DirectorySecurity dSecurity = dInfo. GetAccessControl ();
DSecurity. AddAccessRule (new FileSystemAccessRule (Environment. UserName, FileSystemRights. CreateFiles, AccessControlType. Allow ));
DInfo. SetAccessControl (dSecurity );