Razor Tutoriallogin
Razor Tutorial
author:php.cn  update time:2022-04-11 14:21:21

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 App_Data in the Solution Explorer window Folder
  • SelectAdd, New Item
  • SelectSQL Server Compact Local Database *
  • Name the database Click the Add button for
  • Movies.sdf

* 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 Compact

Visual 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, please visit our SQL Tutorial.


Add database table

Double-click the Movies.sdf file 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 select Create Table.

Create the following columns:

##ColumnID##Titlenvarchar(100)NoDirectornvarchar(100)NoDatedatetimeNo

Explanation 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:

pic_mvc_dbexplorer.jpg

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.


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:

TypeWhether Null is allowed
int (primary key)No
IDTitleDirectorDate
1PsychoAlfred Hitchcock01.01.1960
2 La Dolce VitaFederico Fellini01.01.1960

##Note: ID column will automatically Update, you don't have to edit it.


Add connection strings

Add the following elements to the

<connectionStrings> element in your Web.config file:

<add name="MovieDBContext"
connectionString="Data Source=|DataDirectory|Movies.sdf"
providerName="System.Data.SqlServerCe.4.0"/>