sql server mdf file database attachment - sql-server

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.

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

How to attach a SQL database created under a different account

I created several SQL databases on an external drive using the local admin account on my PC. This PC was then recalled and rebuilt by Head Office.
I have tried and failed to attach these databases to a new install of SSMS.
All of this was done using SQL Server 2016 Developer Edition.
I can created new DBS on the external drive.
I have given my local admin account and the SQL NT Service account (including the SQL Agent one) full access to the folders, the .MDF and the .LDF files.
The error is 'Create file encountered operating system error 5(access is denied.) while attempting to open or create the physical file 'D:|SQLDATA\TESTDB.mdf.' (Microsoft SQL Server, Error: 5123)
Could it be because I didn't detach the databases from the old SQL Server install, which no longer exists?
If anyone can help, I'll be very grateful.
Run Sql as administrator.
or
Right click on file. Propetries > Security.
change the security permissions of ldf , mdf file to full permissions.

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.

Attach Database using Management studio points to the wrong file path for the log file

I am trying to attach a SQL server 2000 MDF and LDF which were created on a different machine and attach them to a SQL Server 2008 R2 on different machine. The SQL server files at the original machine were located at:
C:\DB\SqlServerDataBase.mdf
D:\Logs\SqlServerDatabase.ldf
The 2008 Sql Server machine also contains the same file structure and I place the MDF and LDF files in their appropriate folders:
C:\DB\SqlServerDataBase.mdf
D:\Logs\SqlServerDatabase.ldf
When using the SQL Server Management studio Attach Database option, I point to the MDF file however the log file is automatically is pointed to C:\DB\SqlServerDatabase.ldf instead of D:\Logs\SqlServerDatabase.ldf and subsequently the attach fails since it can not find the log file.
Interesting thing is if I use :
EXEC sp_attach_db #dbname =N'SqlServerDataBase',
#filename1=N'C:\DB\SqlServerDataBase.mdf',
#filename2=N'D:\Logs\SqlServerDatabase.ldf';
The database is restored and pointing to the correct paths for both MDF and LDF.
Thanks
When you're using the GUI (SQL Server Management Studio) and you select Attach Database and go pick a .mdf file, by default SQL Server Mgmt Studio will use the path that the database log file was originally stored on - since that's the only information it has.
But you can and you must change that to match your current setup - just type in the proper path (or select it from the path selector popup when you click on [...] if you prefer that)
Once you've typed in the correct paths, click [OK] and your database will be attached as requested.

Resources