Loading .mdf/.mdv file into SQL Server Management Studio - sql-server

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

Related

MS Visual Studio existing saved sql files cannot access saved database connection from server explorer

I am new to Visual Studio, but I am very familiar with many different development IDEs.
I am using MS Visual Studio Community Edition 2019, and I have a hosted website with a Ms SQL Server 2016 database.
I've connected to the database successfully within Visual Studio and saved the connection in the Server Explorer as a database connection.
It tests successfully, and I am able to launch a query from the server explorer entry for that connection and select/update/insert etc without problem.
When I open an existing saved SQL file, it does not allow the option to connect to my saved connection in the server explorer. The options under both History and Browse, local/network/Azure.... nothing shows my saved database connection.
Is there something I am not aware of, or that I am doing wrong?
Thanks!
Flynn
Once you open a .sql script in Visual Studio; right click in the editor where you see the SQL code, and go to Connection and then use Connect or Change Connection to change where you are connected/connecting to.

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.

Error trying to connect to TFS from SQL Server Management Studio

I'm attempting to connect to a TFS 2010 Server via SQL Server Management Studio 2008. The Source Control has a .sql file I need to open, edit, then commit back to source control.
From SSMS, I go to File > Open From Source Control...
I connect the TFS server and can browse to the project's folders successfully:
However, when I click OK, this error message is returned:
If I connect to TFS via Visual Studio 2008, I can browse the folders on the server, but SSMS will not allow me to. Any idea what I am doing incorrectly?
It sounds like Pero's comments are right, if there are database projects there (presume they have a folder for each project under your DatabaseProject folder) you'd need to select a folder that has a valid *.ssmssln or *.ssmssqlproj in.
Remember that SSMS will only open SQL Script solutions created in SSMS, not SSDT database projects. I don't think there are any projects that you can open in both SSMS and SSDT/Visual Studio.

SQL - How to backup a database and export as a MDF file with MS SQL 2008 R2

I have created a database "test" with some tables in MS SQL Server 2008 R2 (i.e. MS SQL Server Management Studio).
Now, I need to export this database as a MDF file.
What should I do?
If you mean that you want to be able to attach the database on another server, then this is what you can do:
detach the database (right click the database and click Detach)
copy the mdf and ldf files to your backup location
attach the database (right click Databases and click Attach)
This is the path where you will find MDF file:
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\
right-click on database from Management Studio -> Detach. Then you have the MDF file you can export to wherever you want :)
I think the best way is to create a .bak file from SQL Server .
Right click on the database => Task => Back Up -> Choose the database in Source , and hit OK .
If what you want is a replica of your SQLEXPRESS/MDF local file- on a remote server (SQL Server 2005)
You can right click in the "Server Explorer" on your db.mdf file and press "Publish to provider..."
You can choose a variety of compatibilities with SQL Server 2005, 2008, etc.
The output is a .sql query file ..
Came across this question and thought this would be a good alternative answer, since I got here by searching for the exact same thing!
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\
This is the path where you will find MDF file
If you are in Visual Studio, go to SQL Server Object Explorer. Find the database you want, right click and select properties. In the properties tab, look for Data File, next to it will be the location for your mdf file.
In my experience, its easier to use the commands sp_detach_db (MSDN) and sp_attach_db (MSDN). I was trying to attach a database that was offline when detached and SQL Server Management Studio (2014) kept crashing when I used the Attach option from the right-click context menu. The commands worked - hope this saves someone some time.
NOTE: Run SSMS as an administrator if you see an access denied while trying to execute the attach command
Go to the path C:\Users\your pc name
then you will find mdf , ldf file here

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