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

BBoone
  • 51
  • 8
  • 2
    Read the second line of the error message. What do you think it means? – mjwills Oct 30 '18 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. – BBoone Oct 30 '18 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? – mjwills Oct 30 '18 at 22:34
  • Please connect to your local server with Management Studio, and take a screenshot of your list of databases there, so we can confirm the names of all of your database. – mjwills Oct 30 '18 at 23:09
  • 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 – Steve Oct 30 '18 at 23:42
  • `A database with the same name exists` Now, look at your screenshot (under `Databases`). What do you notice? – mjwills Oct 31 '18 at 00:57
  • mjwills so sorry but I am not getting it. I just see one called wrestling – BBoone Oct 31 '18 at 01:29
  • 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? – mjwills Oct 31 '18 at 02:34
  • I did see Steve's comment. I did not understand it. I added the line he said at the end of for string but I still got the same error. "Data Source = (local); AttachDbFilename = C:\APP_DATA\WRESTLING.MDF; Integrated Security = True; Connect Timeout = 30;Initial Catalog=Wrestling;"; – BBoone Oct 31 '18 at 03:23
  • ok I remove the AttachDbFilename line and now I am getting this error: Cannot open database "Wrestling" requested by the login. The login failed. Login failed for user 'IIS APPPOOL\.NET v4.5'. – BBoone Oct 31 '18 at 03:30
  • I have no idea where this 'IIS APPPOOL\.NET v4.5' user is coming from, because I cannot fine it – BBoone Oct 31 '18 at 03:49
  • @BBoone What user does your IIS application run as? How do you think it connects to your database? Have you granted that the permission to do that? May I suggest that this article is worth a read: https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users – Richardissimo Oct 31 '18 at 06:08
  • Note that using `AttachDbFilename` is a [bad practice](https://stackoverflow.com/q/11178720/2557263) and should be avoided. Don't use a lone .mdf file and think it's a DB, use a real DB on the server. – Alejandro Oct 31 '18 at 14:22

1 Answers1

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];
sepupic
  • 8,409
  • 1
  • 9
  • 20
  • check the update, I think I have it that correct user information but I am still getting the same error – BBoone Oct 31 '18 at 14:20
  • "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 Oct 31 '18 at 14:23
  • @ sepupic yes that one – BBoone Oct 31 '18 at 14:35
  • this is because you don't have the database Wrestling. Your database name is WRESTLING.MDF !!! – sepupic Oct 31 '18 at 14:38
  • In the picture your database is called WRESTLING.MDF, not Wrestling. Or rename your database, or rewrite your connection string indicatind initialCatalog = WRESTLING.MDF – sepupic Oct 31 '18 at 14:40