Вы находитесь на странице: 1из 3

Walkthrough: Creating a Local

Database File in Visual Studio

You can explore basic tasks, such as adding tables and defining columns, by using Visual Studio to create
a local database file in SQL Server Express LocalDB. You perform these tasks by using the Table Designer,
which was significantly changed in 2012, and this topic reflects those updates. After you finish this
walkthrough, you can discover more advanced capabilities by using your local database as a starting point
for other walkthroughs that require it.
During this walkthrough, you'll explore the following tasks:
 Create a project and a local database file.
 Create tables, columns, primary keys, and foreign keys.
 Populate the tables with data, and save them.
When you create a service-based database in Visual Studio, 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). See Local Data Overview.

Creating a project and a local database file


To create a project and a database file
1. Create a Windows Forms project that's named SampleDatabaseWalkthrough.
See Creating Solutions and Projects.
2. On the menu bar, choose Project, Add New Item.
The Add New Item dialog box appears so that you can add items that are appropriate in a
Windows Form project.
3. In the list of templates, scroll down until Service-based Database appears, and then choose
it.
4. Name the database SampleDatabase, and then choose the Add button.
The Data
Source Configuration Wizard opens.
5. On the Choose a Database Model page, choose the Dataset icon, and then choose
the Next button.
On the Choose Your Database Objects page, no database objects are available because
the database is new.
6. Choose the Finish button to create the database and dataset and add them to the project.
The properties window for the database shows the connection string and the location of the primary .mdf
file for the database. To display this window, choose the Server
Explorer tab, expand the Data
Connections node, open the shortcut menu for the database, and then choose Properties.
Creating Tables, Columns, Primary Keys, and Foreign Keys
To create the Customers table
1. In Server Explorer/Database Explorer, expand the Data Connections node, and then
expand theSampleDatabase.mdf node.
If Server Explorer doesn't appear, you can display it by choosing View, Server
Explorer on the menu bar.
2. Open the shortcut menu for Tables, and then choose Add New Table.
The Table Designer opens and shows a grid with one default row, which represents a single
column in the table that you're creating. By adding rows to the grid, you'll define additional
columns in the table.
3. In the grid, add a row for each entry in the following table:

Column name Data type Allow nulls

CustomerID nchar(5) False (cleared)

CompanyName nvarchar(40) False (cleared)

ContactName nvarchar (30) True (selected)

Phone nvarchar (24) True (selected)

4. Open the shortcut menu for the CustomerID row, and then choose Set Primary Key.
5. Open the shortcut menu for the default row, and then choose Delete.
6. Name the Customers table by updating the first line in the script pane to match the following
sample:
7. CREATE TABLE [dbo].[Customers]

8. In the upper-left corner of the Table Designer, choose the Update button.
9. In the Preview Database Updates dialog box, choose the Update Database button.
Your changes are saved to the local database file.
To create the Orders table
1. In Server Explorer/Database Explorer, open the shortcut menu for Tables, and then
choose Add New Table.
2. In the grid, add a row for each entry in the following table:

Column name Data type Allow nulls

OrderID int False (cleared)

CustomerID nchar(5) False (cleared)

OrderDate datetime True (selected)

OrderQuantity int True (selected)

3. Open the shortcut menu for the OrderID column, and then choose Set Primary Key.
4. Open the shortcut menu for the default row, and then choose Delete.
5. Name the Orders table by updating the first line in the script pane to match the following sample:
6. CREATE TABLE [dbo].[Orders]

7. In the upper-left corner of the Table Designer, choose the Update button.
8. In the Preview Database Updates dialog box, choose the Update Database button.
Your changes are saved to the local database file.
To create a foreign key
1. In the context pane on the right side of the grid, open the shortcut menu for Foreign Keys,
and then choose Add New Foreign Key.
2. In the text box that appears, replace ToTable with Customers.
3. In the script pane, update the last line to match the following sample:
4. CONSTRAINT [FK_Orders_Customers] FOREIGN KEY ([CustomerID]) REFERENCES
[Customers]([CustomerID])

5. In the upper-left corner of the Table Designer, choose the Update button.
6. In the Preview Database Updates dialog box, choose the Update Database button.
Your changes are saved to the local database file.

Populating the Tables with Data


To populate the tables with data
1. In Server Explorer/Database Explorer, expand the node for the sample database, and
then expand the Tables node.
2. Open the shortcut menu for the Customers table, and then choose Show Table Data.
3. Add whatever data you want for at least three customers.
4. Open the shortcut menu for the Orders table, and then choose Show Table Data.
5. Add data for at least three orders.

Important

Make sure that each value in the CustomerID column of the Orders table matches a value
in the CustomerID column of the Customers table.

6. On the menu bar, choose File, Save


All.
7. On the menu bar, choose File, Close Solution.

Note

As a best practice, you can back up the database file that you just created by copying it
and then either pasting the copy in another location or giving the copy a different name.

Вам также может понравиться