Home > Database > Mysql Tutorial > body text

SQLServer 附加数据库后出现只读或失败的解决方法

WBOY
Release: 2016-06-07 18:00:05
Original
896 people have browsed it

如果你在附加SQL数据库,出现只读或失败的情况,来看下本文的解决方案吧。

解决此问题的几个关键点如下:

1、该现象只会出现在NTFS文件系统中。

2、由NTFS文件系统的访问权限导致。

一般手工操作的解决方案如下:

1、控制面板—>文件夹选项—>查看 关闭“使用简单文件共享(推荐)”

2、选择存放数据库文件的文件夹,右键选择属性,这时会多出一个“安全”选项卡,选择最后一个用户,将该用户的权限设为“完全控制”。

经过这两步操作后,数据库附加失败或附加后只读的问题即可解决。

下面重点讲这两步,用C#代码的实现,因为当我们为项目打包后,不可能让用户手工来执行这些操作

1、C#关闭简单文件共享的代码实现
在百度、Google搜索此问题解决方法时,很多的操作方法并不正确,经过自己实际操作测试,正确方法如下:
文件:
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
如果选中此项,则将上述 0x00000000 改为 0x00000001。

通过修改的方式实现代码如下:
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey software = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Lsa", true);
software.SetValue("forceguest", 0);

2、C#修改指定文件夹的用户访问权限代码实现如下:(将该用户的权限设置为可写)
string path = this.Context.Parameters["targetdir"];//得到文件夹路径
DirectoryInfo dInfo = new DirectoryInfo(path);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.CreateFiles, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!