C# 将数据库(.mdf 文件)附加到 SQL Server 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15929043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error attaching a database (.mdf file) to SQL Server
提问by Butters
I'm having trouble attaching a database DBName.mdf
to a network SQL Server. The admin can manually attach the database but if I try, I get the following error message.
我在将数据库附加DBName.mdf
到网络 SQL Server 时遇到问题。管理员可以手动附加数据库,但如果我尝试,我会收到以下错误消息。
Database 'DBName' cannot be upgraded because it is read-only, has read-only files or the user does not have permissions to modify some of the files. Make the database or files writeable, and rerun recovery. (Microsoft SQL Server, Error: 3415)
无法升级数据库“DBName”,因为它是只读的、具有只读文件或用户没有修改某些文件的权限。使数据库或文件可写,然后重新运行恢复。(Microsoft SQL Server,错误:3415)
Here is my code:
这是我的代码:
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
conn.ConnectionString = @"Server=" + SQLServerName + ";database=master;User ID=" + UserName + ";Pwd=" + Password; ;
try
{
conn.Open();
System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand("CREATE DATABASE DBName ON ( FILENAME = '" + @"C:\DBName.mdf" + "' ), ( FILENAME = '" + @"C:\DBName_log.ldf" + "' ) FOR ATTACH", conn);
com.ExecuteScalar();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
Here is the details of SQL Server
这是 SQL Server 的详细信息
- Product - Microsoft SQL Server Express Edition (64-bit)
- Version - 11.0.2218.0
I can create a new database just fine but cannot attach an existing database. What am I missing here?
我可以很好地创建一个新数据库,但不能附加现有数据库。我在这里缺少什么?
Any help will be appreciated.
任何帮助将不胜感激。
采纳答案by Butters
I figured out what the problem was.
我想出了问题所在。
This link was helpful in figuring out the problem. http://www.sqlservercentral.com/Forums/FindPost1367859.aspx
此链接有助于找出问题。 http://www.sqlservercentral.com/Forums/FindPost1367859.aspx
The user under which the SQL Service was running didn't have full access permissions to that folder. I gave the user full permission and everything worked fine.
运行 SQL 服务的用户没有对该文件夹的完全访问权限。我给了用户完全许可,一切正常。
回答by Butters
I also faced similar situation while using T-SQL Script but i choose another way through SSMS. Here are some easy steps that are very helpful to you to attach SQL MDF file through SQL Server Management Studio.
我在使用 T-SQL 脚本时也遇到了类似的情况,但我通过 SSMS 选择了另一种方式。以下是一些对您通过 SQL Server Management Studio 附加 SQL MDF 文件非常有帮助的简单步骤。
- Go through Start Button->All Programs->SQL Server XXXX->SQL Server Management Studio
- Login in it
- Select Object Explorer enlisted databases
- Then Right click on it and Select Attach database
- Database Attachment Windows appears on the Screen then click on ADD button
- Then you need to select MDF file which you want to attach and click OK
- Confirm it and Click OK
- Database is successfully attached. You can checkout in the Databases list.
- 通过开始按钮->所有程序->SQL Server XXXX->SQL Server Management Studio
- 登入其中
- 选择对象资源管理器登记的数据库
- 然后右键单击它并选择附加数据库
- 数据库附件窗口出现在屏幕上,然后单击添加按钮
- 然后您需要选择要附加的 MDF 文件,然后单击确定
- 确认并单击确定
- 数据库连接成功。您可以在数据库列表中签出。
回答by pblack
I had the same error trying to attach via SQL server management studio.
Running SQL server Management studio as Administrator solved this problem for me.
Info gleaned from here http://www.nickyvv.com/2013/02/database-databasename-cannot-be.html.
我在尝试通过 SQL 服务器管理工作室附加时遇到了同样的错误。
以管理员身份运行 SQL 服务器管理工作室为我解决了这个问题。从这里收集的信息http://www.nickyvv.com/2013/02/database-databasename-cannot-be.html。
回答by Arafat
I also faced the similar problems and I resolved it using both approaches listed above by Butters and pblack. To sum up they are:
我也遇到了类似的问题,我使用 Butters 和 pblack 上面列出的两种方法解决了它。总结起来就是:
- Go to the system services and find SQL Server Database service.
- Right click on the service and select properties
- Go to the Log On tab
If the "Log On As" user from your service is a user account, make sure that user account has Full Control on that folder.
If the "Log On As" user from your service is "Network Service" or "Local System" those account should already have access, but go ahead and add them and give them Full Control.
Lastly, RUN SQL Server management studio using Administrative privileges
- 转到系统服务并找到 SQL Server 数据库服务。
- 右键单击服务并选择属性
- 转到登录选项卡
如果您的服务中的“登录身份”用户是用户帐户,请确保该用户帐户对该文件夹具有完全控制权。
如果您的服务中的“登录身份”用户是“网络服务”或“本地系统”,这些帐户应该已经具有访问权限,但请继续添加它们并授予他们完全控制权。
最后,使用管理权限运行 SQL Server management studio