Monika Galińska
Monika Galińska

Reputation: 201

Can't open database file created by SQL Server in Visual Studio 2013

I create database in SQL Server Management Studio. I tried open database .mdf file in Visual Studio but when I click "test connection", I get error message

Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\database.mdf"
Operating system error 5: "5(Access denied)An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\database.mdf failed.
A database with the same name exists or specified file cannot be opened, or it is located on UNC share

I of course run it administrator, but it not work.

Upvotes: 0

Views: 3449

Answers (3)

Rinoy Ashokan
Rinoy Ashokan

Reputation: 1619

.mdf is stand for master database file. If we select authentication to be handled by Visual Studio itself, then .mdf file and all codes for creating and handling the authentication related tables will be auto generated by visual studio itself (for example you can see this auto generated code by creating a web api by selecting individual account authentication). Auto generated tables are as given below.

  • _MigrationHistory
  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers

And we can go to server explorer to see this table either by double clicking on the created .mdf file or View > Server Explorer. After that Server explorer will come then right click in the any table and select show table data option as the screen shot given below. enter image description here

Then we can see the already created table in mdf database and we can edit the table also if we want. Screen shot given below.

enter image description here

Upvotes: 0

Sunil
Sunil

Reputation: 21406

If you are trying to add a connection to an existing SQL Server database in Visual Studio, then you should not select the option that says Attach a database file. Also, make sure to input the correct Server Name and authentication details.

[ Add Connection Dialog in Visual Studio

You can add a table in Visual Studio just by connecting to database and then right clicking on Tables under the database you have connected. Look at screen shot below.

Create Table in Visual Studio

Upvotes: 1

marc_s
marc_s

Reputation: 754598

You've created the database on the server - where it belongs. Just use it on the server - after all, SQL Server is a server-based solution!

Do NOT fiddle around with .mdf files - let SQL Server handle those. SQL Server is NOT a file-based database like Access, SQLite or FileMaker or any of those - it's server-based (and that's a good thing!).

To talk to your database, just define the necessary parameters:

server=your-machine-name\SQLEXPRESS;database=database-you-created;integrated security=SSPI;

and let SQL Server deal with all the nitty-gritty details. Just use the database as it's intended to be used - as a database under the control of the SQL Server database engine

Upvotes: 1

Related Questions