ASP.NET MVC - SQL Database

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

Part VI: Adding a Database.

Creating the Database

Visual Web Developer accompanies a free SQL database called SQL Server Compact.

The database required for this instructional exercise can be made with these straightforward steps:

  • Right-click the App_Data organizer 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 isn't an alternative, you have not introduced SQL Server Compact on your PC. Introduce it from this connect: SQL Server Compact

Visual Web Developer naturally makes the database in the App_Data folder.

Note: In this instructional exercise it is normal that you have a few learning about SQL databases. In the event that you need to think about this point first, if you don't mind visit our SQL Tutorial.

Adding a Database Table

Double-tapping the Movies.sdf document in the App_Data organizer will open a Database Explorer window.

To make another table in the database, right-click the Tables organizer, and select Create Table.

Create the accompanying columns:

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

Columns explained:

ID is a whole number (entire number) used to recognize each record in the table.

Title is a 100 character content section to store the name of the movie.

Director is a 100 character content section to store the chief's name.

Date is a datetime section to store the discharge date of the movie.

After making the sections portrayed above, you should make the ID segment the table's essential key (record identifier). To do this, click on the segment name (ID) and select Primary Key. Likewise, in the Segment Properties window, set the Identity property to True:

DB Explorer

When you have wrapped up the table segments, spare the table and name it MovieDBs.


We have intentionally named the table "MovieDBs" (finishing with s). In the following part, you will see the name "MovieDB" utilized for the information display. It looks peculiar, yet this is the naming tradition you need to use to make the controller associate with the database table.

Adding Database Records

You can utilize Visual Web Developer to add some test records to the motion picture database.

Double-click the Movies.sdf document 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
La Dolce Vita Federico Fellini 01.01.1960

Note: The ID section is refreshed consequently. You ought not alter it.