Cannot connect to .mdf database

I tried everything, but I can not access / modify / change my database. I created it in visual studio with .mdf. I am new to using SQL database, so I will be glad if you can help me. As I said, I created this database in visual studio, but I cannot create a connection to this database.

In web configuration:

<connectionStrings> <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|Database.mdf;Integrated Security=True;" providerName="System.Data.SqlClient" /> </connectionStrings> 

In cs, I used this sql connection string:

 SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFilename=|DataDirectory|Database.mdf;Integrated Security=True;"); 

And I have this error:

Failed to compile database with auto-name for file C: \ Users \ mcan \ Documents \ Visual Studio 2010 \ WebSites \ WebSite1 \ App_Data \ Database.mdf. A database with the same name exists or the specified file cannot be opened or is located on a UNC share.


I made some changes:

In web configuration:

 <connectionStrings> <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=|DataDirectory|Database.mdf;Integrated Security=True;" providerName="System.Data.SqlClient" /> 

and in cs:

  SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=|DataDirectory|Database.mdf;Integrated Security=True;"); 

Now he gives this error:

 *Server Error in '/WebSite1' Application. Cannot open database |DataDirectory|Database.mdf" requested by the login. The login failed. Login failed for user 'mcan-PC\mcan'. * 
+6
source share
2 answers

As the error clearly states, this database is already connected to SQL Server.
You cannot have two databases with the same name on the same server.

+2
source

The problem may be that your mdf file does not have permissions for Authenticated Users. Go to your .mdf file, right-click and go to "Properties", then the "Security" tab. Check if authenticated users are displayed in "Group or user names." If this does not happen, you need to click “Change”, then “Add” and enter “Authenticated Users”. Then you click "Check Names" and "OK." After that, enable full control for authenticated users. You will also have to repeat this for the .ldf file.

+1
source

Source: https://habr.com/ru/post/907499/


All Articles