How can I install the Northwind database into SQL Server Local DB? - sql-server

I found the following link
Installing the Northwind Sample Database
However I am still not clear on the process. This talks about Server 2000 and MSDE 2000 but what I need is for the new SQL Server Local DB.
Has anyone else had to do this and found any instructions on how to proceed?

So, I can't seem to find a way of attaching an existing DB file to a LocalDB instance from inside visual studio (creating a new DB seems to be the only option), but you can do it via management studio by doing the following:
Step 1 - Find out the instance name of the LocalDB
Open a command prompt and run (the location may be slightly different depending on your install location):
C:\Program Files\Microsoft SQL Server\110\Tools\Binn>SqlLocalDB.exe
info
This will list all the instances of LocalDB, find the one you're interested in, VS2012 installed one for me called v11.0 (which I will use for my example)
Step 2 - Start the LocalDB instance
Again in your command prompt run the following:
C:\Program Files\Microsoft SQL Server\110\Tools\Binn>SqlLocalDB.exe
start v11.0
This will start the LocalDB instance and allow us to get the connection info
Step 3 - Get the name of the named pipe to connect to (pipe name changes every restart)
Another command prompt job:
C:\Program Files\Microsoft SQL Server\110\Tools\Binn>SqlLocalDB.exe
info v11.0
This will then return information about the instance, including the following line:
Instance pipe name: np:\.\pipe\LOCALDB#A6F550C6\tsql\query
Step 4 - Connect via management studio and attach the database
Open management studio and in the server box connect to the named pipe (windows auth) e.g. :
\.\pipe\LOCALDB#A6F550C6\tsql\query
You will now be in the SQL server like it was a normal instance, you can copy your Northwind MDF file to the data directory of the instance (mine was the root of my user folder) and then in the databases node in the management studio right click and select attach, select the MDF file and then attach as normal.
Hope this helps.

How about just restoring via SSMS using Steps:
Download northwind (https://northwinddatabase.codeplex.com/) and save .bak somewhere
Open SSMS
Connect to (localdb)\ProjectsV12 (or whatever version you have)
Right Click Databases Node (see image)
Select Restore...
In Restore Database window, Source | choose 'Device' radio button and in drop down 'Backup media Type' choose File and then click 'Add' button browse to location of downloaded .bak file
That's it, your .bak should be loaded to a new database on your machine.

Related

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.

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.

Backup (Recover) SQL Server Database without SQL Services Running

We've had a harddrive on a PC go belly-up. We were able to partially get it up and running, enough to get files off, but most of the services (including SQL-related services) cannot run. Therefore, we cannot connect via Management Studio.
We do have a data backup, but it's a little old (it's a non-critical bunch of data).
Is there any way to restore a database on a new machine with just the database files?
MSDN for attaching a database (MDB File)
In SQL Server Management Studio Object Explorer, connect to an instance of the Microsoft SQL Server Database Engine, and then expand that instance.
Right-click Databases and click Attach.
In the Attach Databases dialog box, to specify the database to be attached, click Add; and in the Locate Database Files dialog box, select the disk drive where the database resides and expand the directory tree to find and select the .mdf file of the database; for example: C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\AdventureWorks2008R2_Data.mdf
Optionally, to specify a different name for the database to attach as, enter the name in the Attach as column of the Attach Databases dialog box.
Optionally, change the owner of the database by selecting a different entry in the Owner column.
When you are ready to attach the database, click OK.
Sure:
Restore database dbnamegoeshere from disk='path to backup file here'
You can just copy the database files to another machine with SQL services running, and then attach them (right click on Databases, select Attach...)

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