3

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.

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)

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

- 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.

8
  • Could you try to do it "CREATE DATABASE DBName ON ( FILENAME = '" + @"C:\DBName.mdf" + "' ), ( FILENAME = '" + @"C:\DBName_log.ldf" + "' ) FOR ATTACH" manualy? Does it work? Commented Apr 10, 2013 at 14:47
  • It gives the above error if I do it. If the admin of the machine of that SQL Server does it, it works fine. Commented Apr 10, 2013 at 14:54
  • So you need the admin credentials to do it. Are you using here conn.ConnectionString = @"Server=" + SQLServerName + ";database=master;User ID=" + UserName + ";Pwd=" + Password; ; an ADMIN login? Perhaps you have to give your MS SQL Server user more freedom :) Commented Apr 10, 2013 at 14:55
  • Basically. But why? I can create database on that Server Just fine. Why do I need admin privilege for attaching a database? Commented Apr 10, 2013 at 14:57
  • Have a read here msdn.microsoft.com/en-us/library/ms190209(v=sql.105).aspx and here social.msdn.microsoft.com/Forums/en-US/sqlexpress/thread/… Commented Apr 10, 2013 at 14:59

4 Answers 4

17

I figured out what the problem was.

This link was helpful in figuring out the problem. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent solution. After 3 hours of trying to move Enterprise Vault SQL database files I added the SQL Server Service logon account to the destination folder and bingo was able to attach the databases. Thank you
7

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.

Comments

1

I also faced the similar problems and I resolved it using both approaches listed above by Butters and pblack. To sum up they are:

  • 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

Comments

0

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.

  1. Go through Start Button->All Programs->SQL Server XXXX->SQL Server Management Studio
  2. Login in it
  3. Select Object Explorer enlisted databases
  4. Then Right click on it and Select Attach database
  5. Database Attachment Windows appears on the Screen then click on ADD button
  6. Then you need to select MDF file which you want to attach and click OK
  7. Confirm it and Click OK
  8. Database is successfully attached. You can checkout in the Databases list.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.