1

I am getting this error:

CREATE DATABASE permission denied in database 'master'.

An attempt to attach an auto-named database for file C:\APP_DATA\WRESTLING.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

but I give my service the administrator account:

enter image description here

So, why is it being denied?

Here is my code:

void ConnectToDb()
{
        connStringBuilder = new SqlConnectionStringBuilder();
        connStringBuilder.DataSource = @"(localdb)\MSSQLLocalDB";
        connStringBuilder.InitialCatalog = "WRESTLING.MDF";
        connStringBuilder.Encrypt = true;
        connStringBuilder.ConnectTimeout = 30;
        connStringBuilder.AsynchronousProcessing = true;
        connStringBuilder.MultipleActiveResultSets = true;
        connStringBuilder.IntegratedSecurity = true;

        string temp = @"Server=EC2AMAZ-FN5N011\MSSQLSERVER;Database=C:\APP_DATA\WRESTLING.MDF;Trusted_Connection=True;";
        string temp1 = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=C:\APP_DATA\WRESTLING.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        string temp2 = @"Data Source = (local); AttachDbFilename = C:\APP_DATA\WRESTLING.MDF; Integrated Security = True; Connect Timeout = 30;";

        conn = new SqlConnection(temp2);
        comm = conn.CreateCommand();
}

Also, I am using an IIS Service to connect to the SQL database and that IIS is an Administrator too .

Update:

[enter image description here2

[enter image description here3

13
  • 2
    Read the second line of the error message. What do you think it means? Commented Oct 30, 2018 at 22:27
  • I am not sure. I know the file is located there, and there is only one Wrestling.MDF file in the folder. I do not know what UNC share is. Commented Oct 30, 2018 at 22:33
  • A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. Does a database with the same name exist? Can the SQL Server service access the file? Is it located on a UNC share? Commented Oct 30, 2018 at 22:34
  • 1
    Add a logical name for your database, In the last connection string add "Initial Catalog=Wrestling;" (no extension) If you don't specify the catalog name then your file path becomes the logical name. Remove the current logical name using SSMS Commented Oct 30, 2018 at 23:42
  • 1
    You see one called wrestling.mdf. You are adding one called wrestling.mdf. The error explicitly told you that you did that. Did you see the comment from @Steve above? Commented Oct 31, 2018 at 2:34

1 Answer 1

1

Your code is the mix of connection strings to different servers. So it's not clear to which one you want to connect.

string temp and string temp2 attempt to connect to the default local instance of SQL Server. You should not attach nothing to it as there is already the database in question attached to this instance.

Your string temp1 attempts to connect to localdb, it's another server, not that one that we see on your screenshot, and here yes you can specify the file to attach.

Now it seems that you are trying to connect to the default instance under IIS APPPOOL\.NET v4.5 (your IIS is running under this account), this account is not the same with that you used to connect at the screenshot.

You should map this login to server (for now it's not mapped or at least not explicitely) and then map it to your database and make it db_owner:

create login [IIS APPPOOL\.NET v4.5] from windows;

use WRESTLING;
go

create user [IIS APPPOOL\.NET v4.5] from login [IIS APPPOOL\.NET v4.5];
exec sp_addrolemember 'db_owner', [IIS APPPOOL\.NET v4.5];
Sign up to request clarification or add additional context in comments.

5 Comments

check the update, I think I have it that correct user information but I am still getting the same error
"The same error" -- what error? Cannot open database "Wrestling" requested by the login. The login failed. Login failed for user 'IIS APPPOOL\.NET v4.5'?
@ sepupic yes that one
this is because you don't have the database Wrestling. Your database name is WRESTLING.MDF !!!
In the picture your database is called WRESTLING.MDF, not Wrestling. Or rename your database, or rewrite your connection string indicatind initialCatalog = WRESTLING.MDF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.