MVC database
ASP.NET MVC - SQL Database
To learn ASP.NET MVC, we will build an Internet application.
Part 6: Add the database.
Create Database
Visual Web Developer comes with a free SQL database called SQL Server Compact.
The database required for this tutorial can be created by following a few simple steps:
Right-click Solution Explorer in the window ##App_Data Folder
##Select- Add, New Item
- SQL Server Compact Local Database *
- Movies.sdf
- Add
Button
If there is no SQL Server Compact Local Database in the options, you have not installed SQL Server Compac on your computer. Please install via the following link: SQL Server CompactVisual Web Developer automatically creates the database in the App_Data folder.
Note:In this tutorial, you need to master some basic knowledge about SQL database. If you want to learn this topic first, visit our SQL tutorial.
Add database tableDouble-click the
Movies.sdffile in the App_Data folder and Database Explorer## will open # window. To create a new table in the database, right-click the Tables
folder and selectCreate Table. Create the following columns:
##ColumnTypeint (primary key)##Titlenvarchar(100)NoDirectornvarchar(100)NoDatedatetimeNoExplanation of columns:
ID is an integer (full number) used to identify each record in the table.
Title is a 100-character text column that stores the title of the movie.
Director is a 100-character text column that stores the director's name.
Date is the date column used to store the release date of the movie.
After creating the above columns, you must set the ID column as the primary key (record identifier) of the table. To do this, click on the column name (ID) and select Primary Key. In the Column Properties window, set the Identity property to True:
When you create the table After the columns, save the table and name it MovieDBs.
Note:
We intentionally named the table "MovieDBs" (ending with s). In the next chapter, you will see "MovieDB" for the data model. This may seem a bit strange, but this naming convention ensures that the controller is connected to the database table, and you must use it this way.
Add 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:
Whether Null is allowed | ID | |
---|---|---|
No | ||
ID | Title | Director | Date |
---|---|---|---|
1 | Psycho | Alfred Hitchcock | 01.01.1960 |
2 | La Dolce Vita | Federico Fellini | 01.01.1960 |
##Note: ID column will automatically Update, you don't have to edit it.
Add connection stringsAdd the following elements to the
<connectionStrings> element in your Web.config file:
providerName="System.Data.SqlServerCe.4.0"/>