Attaching a database to SQL Server (Management Studio) - Error 5 - sql-server

I am trying to attached a database from a previous company to a new SQL Server. The .mdf and .ldf files are both saved to my desktop.
When I try to attach the database, I get the following error:
I have attached this database before, with no problem.

Related

Loading .mdf/.mdv file into SQL Server Management Studio

I downloaded SQL Server Management Studio to my laptop at home. I don't have a database as it is my personal PC. Is it possible to connect to a .mdf file and do SQL querying in that ?
Let's say someone gives you an MDF file of which that's the only database file other than the log file. Can you "connect" to it?
Yes...
Let's say the .MDF file is here: D:\SQL\MSSQL12.MSSQLSERVER\MSSQL\DATA\DatabaseMDF.mdf
In SSMS, connect object explorer to your SQL Server. Right click the Databases folder, and select Attach Database. Add the file, D:\SQL\MSSQL12.MSSQLSERVER\MSSQL\DATA\DatabaseMDF.mdf. Verify that you have legal paths in the database details sub pane under the Databases to attach sub pane. Also, in the database details sub pane, remove the log file you do not have. Click OK... and you have the DB available.
You will need SQL Server installed and running as well. Just SSMS (SQL Server Management Studio isn't enough). You can download from here...
https://www.microsoft.com/en-us/download/details.aspx?id=42299
Windows 7
https://www.microsoft.com/en-us/download/details.aspx?id=55994
Windows 10

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.
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.
[
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.
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
.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.
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.

How do I reattach a LocalDB database within VS2013 for further schema development?

A couple of days ago I created (my first) localdb database from within Visual Studio 2013. After defining the schema, I detached the db files from the server, within Visual Studio, so that I could move them to another project folder. Now, for the life of me, I can not figure out how to re-attach the db so that I can update the schema.
I have been able to add the file to the Server Explorer in VS, and I can browse tables and the data. However, from within the SQL Server Object Explorer window, I can not figure out how to get back my database. How can I add it/re-attach it again so I can modify the db schema?
Open a new query on that localdb instance and run the following (modify for your Database and FilePath specifics):
CREATE DATABASE DBname
ON (FILENAME =
'C:\Users\{Login}\AppData\Local\Microsoft\VisualStudio\SSDT\DBname\DBname.mdf'),
(FILENAME =
'C:\Users\{Login}\AppData\Local\Microsoft\VisualStudio\SSDT\DBname\DBname.ldf')
FOR ATTACH;
It worked as expected for me. I detached a database and reattached it via the above command.
PLEASE NOTE: The O.P. reported back:
I had to disconnect from the Server Explorer window. Otherwise, I was getting a "File Already In Use" error message.

SQL Server Management Studio error 948

I tried to attach a database (.mdf) file and I got this error:
Attach database failed for Server '...\SQLINSTANCE'. (Microsoft.SqlServer.Smo)
Additional information:
An exception occured while executing a Transact-SQL statement or batch.
(Microfost.SqlServer.ConnectionInfo)
The database 'DBName' cannot be opened because it is version 782. This server supports version 706 or earlier. A downgrade path is not supported.
Could not open new database 'DBName'. CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 948)
P.S. I have installed SQL Server 2012 Express, and database 'DBName' has been made in Visual Studio 2012.
If you have source control
Rollback the mdf file
Rollback the idf file (log file) to the same version
I had a similar problem and this solution fixed is.
If you do not have source control and you are using Code First
Backup the mdf and idf files
Delete the mdf and idf files in the solution explorer
Create a new database from project->add new item->data->SQL server database
In the package manager console write "Enable-migrations" then "Add-migration init" then "Update-database" to create your tables in the database.
For me, if things got too complicated, I would just recreate the project and copy the code :)

sql server mdf file database attachment

I'm having a bear of a time getting visual studio 2010 (ultimate I think) to properly attach to my database. It was moved from it's original spot to #MYAPP#/#MYAPP#.Web/App_Data/#MDF_FILE#.mdf. I have three instances of SQL Server running on this machine. I have tried to replace the old mdf file with my new one and cannot get the connectionstring right for it.
What I'm really trying to do is to just open a DB instance and run a DB create script. Then I can have a DB that was generated via my edmx (generate database from model) in Silverlight business application (c#)
Right now, when I go to Server Explorer in VS, choose add new connection, choose MS SQL Server Database File (SqlClient), choose my file location (app_data directory), use Windows authentication, and hit the Test Connection button, I get the following error:
Unable to open the physical file "". Operating system error 5: "5(Access Denied.)". An attempt to attach to an auto-named database for file"" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
The mdf file was created on the same machine by
Connecting to (local) in SQL Server management studio.
Getting a new query.
Pasting in the SQL from the generated ddl file.
Adding CREATE DATABASE [NcrCarDatabase]; GO; before the pasted SQL and
Executing the query.
I then
Disconnected from the DB in Management Studio.
Closed Management Studio
Navigated to the DATA directory for that instance
Copied the mdf and ldf files to my application's app_data folder.
I am now trying to connect to the same file inside visual studio.
I hope that gives more clarity to my problems :).
Connection string is:
Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\SourceCode\NcrCarDatabase\NcrCarDatabase.Web\App_Data\NcrCarDatabase.mdf;Integrated
Security=True;Connect Timeout=30;User
Instance=True
I was getting "access denied" until I ran Visual Studio as an administrator. I then had to detach the database from MSSMS before I could get it into Visual Studio.
You mentioned that your MDF file was moved. Was the MDF properly attached to SQL Express after the move? See: How to: Attach a Database File to SQL Server Express.
Access denied might well mean that you already have an active instance that has opened the file prior to you doing it - and if you have three active instances, it is likely one of them that is the culprit.
You can try to connect to the different instances with Management Studio and see if you can find the base there. If yes, the detach it from the instance and re-try from Visual Studio. Or, if possible, try to shut down all three instances and then re-try from Visual Studio. If that works, restart the instances one by one to determine which of them is trying to get the file.
I've previously answered a similar question that might help.
Your Sql server service runs under a given account. You should determine the account through the service properties then change the db file security access to full access for that account.
Give Security to Full control for your application Folder and .mdf, .ldf files. Write click on the folder which contains .mdf,.ldf files
Go to property
click security in that
Click Full Control in allow.
and them proceed your process.

Resources