Walkthrough: Creating a LocalDB Database
By following this walkthrough, you can create a SQL Server database file based on the Customers and Orders tables in the Northwind Sample database. You can then use the file that you created to learn more about local database files by completing other how-to and walkthrough tasks in this Help system.
During this walkthrough, you will learn how to:
-
Create database tables and relationships using Visual Database Tools . For more information, see Visual Database Tools.
When creating a new service-based database in Visual Studio 2012, the SQL Server Express LocalDB engine is used to access a SQL Server 2012 database file (.mdf). In earlier versions of Visual Studio, the SQL Server Express engine is used to access a database file (.mdf). For more information, see Local Data Overview.
In order to complete this walkthrough, you will need:
-
Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
Because this walkthrough creates a database based on the empty database template, you must create a project to create the database in.
To create the new Windows project
-
In Visual Studio, choose File, New, Project.
-
Name the project SampleDatabaseWalkthrough.
-
Select Windows Forms Application and choose the OK button. For more information, see Developing Client Applications.
The SampleDatabaseWalkthrough project is created and added to Solution Explorer.
To add a new database to the project
-
Choose Project, Add New Item.
-
Select Service-based Database from the list of available templates.
-
In the Name area, type SampleDatabase.
-
Choose the Add button. The Data Source Configuration Wizard opens.
-
On the Choose a Database Model page, select Dataset, and then choose the Next button.
On the Choose Your Database Objects page, no database objects are available because the database is new.
-
Choose the Finish button to create the database and dataset and add them to the project.
To display the connection string and the location of the primary .mdf file for the database in Server Explorer, open the shortcut menu for the database, and then choose Properties.
Note |
|---|
| A new version of Table Designer appears for databases in the SQL Server 2012 format. This topic describes the old version of Table Designer, which you use with databases in earlier formats of SQL Server. In the new version, you can change a table definition through a graphical interface or directly in a script pane. If you use the graphical interface, the table’s definition is automatically updated in the script pane. To apply the SQL code in the script pane, choose the Update button. For more information about the new version, see How to: Create Database Objects Using Table Designer. |
To add tables to the database
Open Server Explorer/Database Explorer by choosing View, Server Explorer/Database Explorer.
Expand the SampleDatabase.mdf node under the Data Connections node.
Open the shortcut menu for Tables and choose Add New Table.
The Table Designer opens.
Create columns with the following information:
Column name
Data type
Allow nulls
CustomerID
nchar(5)
False (not checked)
CompanyName
nvarchar(40)
False (not checked)
ContactName
nvarchar (30)
True (checked)
ContactTitle
nvarchar (30)
True (checked)
Address
nvarchar (60)
True (checked)
City
nvarchar (15)
True (checked)
Region
nvarchar (15)
True (checked)
PostalCode
nvarchar (10)
True (checked)
Country
nvarchar (15)
True (checked)
Phone
nvarchar (24)
True (checked)
Fax
nvarchar (24)
True (checked)
Select the CustomerID column, and then choose Table Designer, Set Primary Key.
Choose File, Save Table1.
Type Customers in the Enter a name for the table area.
Choose the OK button.
Open the shortcut menu for Tables and choose Add New Table.
The Table Designer opens.
Create columns with the following information:
Column name
Data type
Allow nulls
OrderID
int
False (not checked)
CustomerID
nchar(5)
True (checked)
EmployeeID
int
True (checked)
OrderDate
datetime
True (checked)
RequiredDate
datetime
True (checked)
ShippedDate
datetime
True (checked)
ShipVia
int
True (checked)
Freight
money
True (checked)
ShipName
nvarchar(40)
True (checked)
ShipAddress
nvarchar(60)
True (checked)
ShipCity
nvarchar(15)
True (checked)
ShipRegion
nvarchar(15)
True (checked)
ShipPostalCode
nvarchar(10)
True (checked)
ShipCountry
nvarchar(15)
True (checked)
Select the OrderID column, and then choose Table Designer, Set Primary Key.
Choose File, Save Table2.
In the Enter a name for the table area, type Orders.
Choose the OK button.
To create a relationship between the tables created in the previous step
In Server Explorer/Database Explorer, in the SampleDatabase.mdf node, open the shortcut menu for Database Diagrams, and then choose Add New Diagram.
If a dialog box opens asking to create the required database objects for diagramming, choose the Yes button.
Add the Customers and Orders tables to the diagram.
Close the Add Table dialog box.
Drag the CustomerID column from the Customers table onto the Orders table.
Verify that the Customers table is the Primary key table and the Orders table is the Foreign key table, and then verify that the CustomerID column is selected for both tables.
Choose the OK button to close the Tables and Columns dialog box.
Choose the OK button to close the Foreign Key Relationship dialog box and create the relationship.
Choose File, Save Diagram1.
Leave the default name and choose OK button.
In the Save dialog box, choose the Yes button.
To populate the Customers table with data
-
Create a new connection to the Northwind sample database in Server Explorer/Database Explorer. For more information, see How to: Install Sample Databases and How to: Connect to Data in a Database.
-
In Server Explorer/Database Explorer, expand the Northwind database node.
-
Open the shortcut menu for the Northwind Customers table, and choose Show Table Data.
-
Select all the records and copy them to the Clipboard.
-
In Server Explorer/Database Explorer, expand the SampleDatabase.mdf database.
-
Open the shortcut menu for the SampleDatabase.mdf Customers table, and choose Show Table Data.
-
Paste the Northwind Customers table data from the Clipboard.
To populate the Orders table with data
-
Open the shortcut menu for the Northwind Orders table, and choose Show Table Data.
-
Select all the records and copy them to the Clipboard.
-
In Server Explorer/Database Explorer, expand the SampleDatabase.mdf database.
-
Open the shortcut menu for the SampleDatabase.mdf Orders table, and choose Show Table Data.
-
Paste the Northwind Orders table data from the Clipboard.
Now that you have created a sample database with data, you should make a copy of the database in its original state so you can restore the data if needed.
To save the sample database
-
Choose File, Save All.
-
Choose File, Close Solution.
-
In your project folder, browse to the SampleDatabase.mdf file, and Copy it.
-
Browse to a folder where you want to save the database and paste the copy into the folder.
Note