ASP.NET MVC Database

## ASP.NET Tutorial

ASP.NET HOME ASP.NET Intro

## WP Tutorial

WebPages Intro WebPages Razor WebPages Layout WebPages Folders WebPages Global WebPages Forms WebPages Objects WebPages Files WebPages Databases WebPages Helpers WebPages WebGrid WebPages Charts WebPages Email WebPages PHP WebPages Publish WebPages Examples

## WP References

WebPages Classes WebPages Security WebPages Database WebPages WebMail WebPages Helpers

## ASP.NET Razor

Razor Intro Razor Syntax Razor C# Variables Razor C# Loops Razor C# Logic Razor VB Variables Razor VB Loops Razor VB Logic

## ASP.NET MVC

MVC Intro MVC Application MVC Folders MVC Layout MVC Controllers MVC Views MVC Database MVC Models MVC Security MVC HTML Helpers MVC Publish MVC Reference

## WF Tutorials

WebForms Intro WebForms Pages WebForms Controls WebForms Events WebForms Forms WebForms ViewState WebForms TextBox WebForms Button WebForms Data Binding WebForms ArrayList WebForms Hashtable WebForms SortedList WebForms XML Files WebForms Repeater WebForms DataList WebForms DbConnection WebForms Master Pages WebForms Navigation WebForms Examples

## WF References

WebForms HTML WebForms Controls WebForms Validation

# ASP.NET MVC - SQL Database

« Previous
Next Chapter »

To learn ASP.NET MVC, we are Building an Internet Application.

Part VI: Adding a Database.

## Creating the Database

Visual Web Developer comes with a free SQL database called SQL Server Compact.

The database needed for this tutorial can be created with these simple steps:

- Right-click the App_Data folder in the Solution Explorer window
- Select Add, New Item
- Select SQL Server Compact Local Database *
- Name the database Movies.sdf.
- Click the Add button

* If SQL Server Compact Local Database is not an option, you have not installed SQL Server Compact on your computer. Install it from this link: SQL Server Compact

Visual Web Developer automatically creates the database in the App_Data folder.

Note: In this tutorial it is expected that you have some knowledge about SQL databases. If you want to study this topic first, please visit our SQL Tutorial.

## Adding a Database Table

Double-clicking the Movies.sdf file in the App_Data folder will open a Database Explorer window.

To create a new table in the database, right-click the Tables folder, and select Create Table.

Create the following columns:

Column | Type | Allow Nulls
--- | --- | ---
ID | int (primary key) | No
Title | nvarchar(100) | No
Director | nvarchar(100) | No
Date | datetime | No

Columns explained:

ID is an integer (whole number) used to identify each record in the table.

Title is a 100 character text column to store the name of the movie.

Director is a 100 character text column to store the director's name.

Date is a datetime column to store the release date of the movie.

After creating the columns described above, you must make the ID column the table's primary key (record identifier). To do this, click on the column name (ID) and select Primary Key. Also, in the Column Properties window, set the Identity property to True:

When you have finished creating the table columns, save the table and name it MovieDBs.

Note:

We have deliberately named the table "MovieDBs" (ending with s). In the next chapter, you will see the name "MovieDB" used for the data model. It looks strange, but this is the naming convention you have to use to make the controller connect to the database table.

## Adding Database Records

You can use Visual Web Developer to add some test records to the movie database.

Double-click the Movies.sdf file in the App_Data folder.

Right-click the MovieDBs table in the Database Explorer window and select Show Table Data.

Add some records:

ID | Title | Director | Date
--- | --- | --- | ---
1 | Psycho | Alfred Hitchcock | 01.01.1960
2 | La Dolce Vita | Federico Fellini | 01.01.1960

Note: The ID column is updated automatically. You should not edit it.

## Adding a Connection String

Add the following element to the <connectionStrings> element in your Web.config file:

<add name="MovieDBContext"
connectionString="Data Source=|DataDirectory|\Movies.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE