Solution to read-only or failure after attaching a database to SQLServer

Source: Internet
Author: User

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:
Registry file:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Lsa]
"Forceguest" = dword: 00000000
Batch Processing:
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 for modifying the registry 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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.