Solutions and code implementation for read-only or failure after attaching an SQL Server database

Source: Internet
Author: User

I believe that anyone who has used SQL server will have this experience. When we attach an SQL Server database, the append will fail, or the database will be read-only after the append is successful.

This may also cause the failure to automatically append the database in the installation and deployment project.

I remember that I had been troubled by this problem for a long time, and I have not provided complete and detailed solutions on the Internet. I would like to share my experience with you.

 

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.