简体   繁体   中英

Error attaching a database (.mdf file) to SQL Server

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.

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.

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 .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM