Reputation: 997
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.
Upvotes: 3
Views: 35034
Reputation: 1400
I also faced the similar problems and I resolved it using both approaches listed above by Butters and pblack. To sum up they are:
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
Upvotes: 1
Reputation: 808
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.
Upvotes: 7
Reputation:
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.
Upvotes: 0
Reputation: 997
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.
Upvotes: 17