1. Trang chủ
  2. » Luận Văn - Báo Cáo

Lecture E-Commerce - Chapter 24: ASP.NET (part III)

68 123 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 68
Dung lượng 508,67 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Lecture E-Commerce - Chapter 25: ASP.NET MVC. In this chapter students will be able to: Introduction, MVC application, MVC folders, MVC layout, MVC controllers, MVC views, MVC database, MVC models.

Trang 1

CSC 330 E-Commerce

Teacher

Ahmed Mumtaz Mustehsan

GM-IT CIIT Islamabad

Virtual Campus, CIIT

COMSATS Institute of Information Technology

T2-Lecture-12

Trang 2

ASP.NET Part - III

For Lecture Material/Slides Thanks to: www.w3schools.com

Trang 3

ASP.NET Part III WebPages (Continued)

Introduction to SQL

Thank You

Trang 5

WebPages Databases

Trang 6

Displaying Data from Database

With Web Pages, data can easily be displayed from a database

Connect to an existing database, or create a new

database from scratch

The following example will demonstrate to connect to

an existing SQL Server Compact database

Trang 7

Displaying Data from Database

<h1>Small Bakery Products</h1>

<table border="1" width="100%">

Trang 8

Output of the above Example

2 Strawberry Cake Made with organic

5 Lemon Pie Made with the best

lemons in the world 11.99

Small Bakery Products

Trang 9

Example Explained

The Database.Open(name) method will connect to a

database in two steps:

◦First, it searches the application's App_Data folder

for a database that matches the name (Small bakery) parameter without the file-name extension

◦If no file is found, it looks for a "connection string" in the application's Web.config file

(A connection string contains information about how to connect to a database? It can include a file path, or the

password)

This two-step search makes it possible to test the

Trang 10

WebPages Helpers

Trang 11

ASP.NET Web Pages - Helpers

Web Helpers greatly simplifies web development and common programming tasks

ASP.NET Helpers

ASP.NET helpers are components that can be

accessed by single lines of Razor code

You can build your own helpers using Razor syntax

stored as cshtml files, or use built-in ASP.NET helpers

Trang 12

Razor helpers: The WebGrid Helper

Automatically sets up an HTML table to display data

Supports different options for formatting

Supports paging (First, next, previous, last) through

data

Supports sorting by clicking on column headings

Trang 13

The Chart Helper

The "Chart Helper" can display chart images of

different types with many formatting options and labels

Trang 14

The WebMail Helper

email messages using SMTP (Simple Mail Transfer

Protocol)

Trang 15

The WebImage Helper

and manipulate images in a web page

Trang 16

WebPages WebGrid

Trang 17

ASP.NET Web Pages - The WebGrid Helper

WebGrid - One of many useful ASP.NET Web Helpers

We have already learnt to display database data by

using razor code, and using the HTML markup

Trang 18

ASP.NET Web Pages

( Displaying Data from Database previous Method)

Database Example

@{

var db =

Database.Open("SmallBakery");

var selectQueryString = "SELECT *

FROM Product ORDER BY Name";

Trang 19

Output of the above Example

2 Strawberry Cake Made with organic

5 Lemon Pie Made with the best

lemons in the world 11.99

Small Bakery Products

Trang 20

Using The WebGrid Helper

Using the WebGrid helper is an easier way to display data from Database

The WebGrid helper:

◦Automatically sets up an HTML table to display data

◦Supports different options for formatting

◦Supports paging through data

◦Supports Sorting by clicking on column headings

Trang 22

WebGrid Example - Output

Small Bakery Products

Id Name Description Price

1 Bread Baked fresh every day 2,99

3 Pecan Pie If you like pecans, this is

Trang 23

The Chart Helper

The "Chart Helper" can display chart images of

different types with many formatting options and labels

It can create standard charts like area charts, bar

charts, column charts, line charts, and pie charts, along with more specialized charts like stock charts

Trang 24

The Chart Helper – Chart from an Array

Trang 25

The Chart Helper

new Chart creates a new chart object and sets its width and height

Trang 26

WebPages Email

Trang 27

ASP.NET Web Pages

The WebMail Helper

The WebMail Helper makes it easy to send an email from a web application using SMTP (Simple Mail

transfer Protocol)

To demonstrate the use of email, we will create an

input page for support, let the user submit the page to another page, and send an email about the support

problem

Trang 28

First: Edit Your AppStart Page

Add in the page called _AppStart.cshtml (already

learnt) contain the following contents:

Trang 29

First: Edit Your _AppStart Page

Add the following entries:

 SmtpServer: The name the SMTP server that will be used to

send the emails.

 SmtpPort: The port the server will use to send SMTP

transactions (emails).

 EnableSsl: True, if the server should use SSL (Secure

Socket Layer) encryption False otherwise

 UserName: The name of the SMTP email account used to

send the email.

 Password: The password of the SMTP email account.

same as UserName).

Trang 30

First: Edit Your AppStart Page

How to add the WebMail properties to AppStart page:

Trang 31

Second: Create an Email Input Page

Then create an input page, and name it Email_Input:

Email_Input.cshtml

<!DOCTYPE html>

<html>

<body>

<h1>Request for Assistance</h1>

<form method="post" action="EmailSend.cshtml">

<label>Username:</label>

<input type="text name="customerEmail" />

<label>Details about the problem:</label>

<textarea name="customerRequest" cols="45" rows="4"></textarea>

<p><input type="submit" value="Submit" /></p>

</form>

</body>

</html>

Trang 32

Third: Create An Email Send Page

Then create the page that will be used to send the email, and name

it Email_Send:

Email_Send.cshtml

@{ // Read input

var customerEmail = Request["customerEmail"];

var customerRequest = Request["customerRequest"];

try

{

// Send email

WebMail.Send(to:" support@vcomsats.edu.pk ", subject: "Help

request from - " + customerEmail, body: customerRequest );

Trang 33

WebPages Publish

Trang 34

ASP.NET Web Pages - Publishing the Website

Publish Your Application Without Using WebMatrix

An ASP.NET Web Pages application can be published

to a remote server by using the Publish commands in

WebMatrix (or Visual Studio)

This function copies all your application files, cshtml

pages, images, and all the required DLL files for Web

Pages, for Razor, for Helpers, and for SQL Server

Compact (if a database is used)

Trang 35

Publish Your Application Without Using

WebMatrix

To perform a web copy, you need to know how to

include the right files, what DDL files to copy, and

where store them

Follow these steps:

1 Use the Latest Version of ASP.NET

Before you continue, make sure your hosting computer runs the latest version of ASP.NET (4.0 or 4.5)

2 Copy the Web Folders

Copy your website (all folders and content) from your development computer to an application folder on your remote hosting computer (server)

Trang 36

Publish Your Application Without Using

Trang 37

Publish Your Application Without Using

WebMatrix

4 Copy Your Data

If your application contains data or a database For

instance an SQL Server Compact database (a sdf file in App_Data folder), consider the following:

Do you want to publish your test data to the remote

server?

Usually not (We do testing locally before publishing)

If you have test data on your development computer, it may overwrite production data on your remote hosting

computer

If you have to copy an SQL database (.sdf file), you

may delete everything in the database, and then copy the

Trang 38

The End ASP.NET

Trang 39

Introduction to SQL

Trang 40

What is SQL?

SQL stands for Structured Query Language

SQL lets you access and manipulate databases

SQL is an ANSI (American National Standards

Institute) standard

Trang 41

What Can SQL do?

SQL can execute queries against a database

SQL can retrieve data from a database

SQL can insert records in a database

SQL can update records in a database

SQL can delete records from a database

SQL can create new databases

SQL can create new tables in a database

SQL can create stored procedures in a database

SQL can create views in a database

SQL can set permissions on tables, procedures, and views

Trang 42

Using SQL in Your Web Site

To build a web site that shows data from a database, you will need:

An RDBMS database program (i.e MS Access, SQL Server, MySQL)

To use a server-side scripting language, like PHP or ASP

To use SQL to get the data you want

To use HTML / CSS

Trang 43

RDBMS stands for Relational Database Management System

RDBMS is the basis for SQL, and for all modern

database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access

The data in RDBMS is stored in database objects

called tables

A table is a collection of related data entries and it

consists of columns and rows

Trang 44

Database Table

A database most often contains one or more tables

Each table is identified by a name (e.g "Customers" or

"Orders") Tables contain records (rows) with data

In this Lecture we will use the sample database picked

up from W3schools Tutorial

Below is a selection from the "Customers" table:

Emparedados y helados

Constitución 2222

Taquería

Trang 45

SELECT * FROM Customers;

Keep in Mind That

SQL is NOT case sensitive: select is the same as

SELECT

Trang 46

The Most Important SQL Commands

Trang 47

The SQL SELECT Statement

The SELECT statement is used to select data from a database

The result is stored in a result table, called the set

result-SQL SELECT Syntax

SELECT column_name,column_name FROM table_name;

SELECT * FROM table_name;

Trang 48

Semicolon after SQL Statements?

Some database systems require a semicolon at the

end of each SQL statement

Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server

Use semicolon at the end of each SQL statement

Trang 49

Demo Database

sample database

Emparedados y helados

Constitución 2222

Taquería

snabbköp

Trang 50

SELECT Column Example

The following SQL statement selects the

"CustomerName" and "City" columns from the

Trang 51

The SQL SELECT DISTINCT Statement

In a table, a column may contain many duplicate

values; and sometimes you only want to list the

different (distinct) values

distinct (different) values

SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name,column_name

FROM table_name;

Or

SELECT DISTINCT City FROM Customers;

The above SQL statement selects only the distinct

values from the "City" columns from the "Customers"

Trang 52

The SQL WHERE Clause

The WHERE clause is used to extract only those

records that fulfill a specified criterion

SQL WHERE Syntax

SELECT column_name,column_name FROM table_name

WHERE column_name operator value;

Example

SELECT * FROM Customers WHERE Country='Mexico';

The above SQL statement selects all the customers from the country "Mexico", in the "Customers" table:

Trang 53

Text Fields vs Numeric Fields

SQL requires single quotes around text values (most database systems will also allow double quotes)

However, numeric fields should not be enclosed in

quotes:

Example

SELECT * FROM Customers WHERE CustomerID=1;

Trang 54

Operators in The WHERE Clause

The following operators can be used in the WHERE

Trang 55

The SQL AND & OR Operators

The AND operator displays a record if both the first

condition AND the second condition are true

The OR operator displays a record if either the first

condition OR the second condition is true

AND Operator Example

SELECT * FROM Customers WHERE

Country='Germany‘ AND City='Berlin';

The above SQL statement selects all customers from the country "Germany" AND the city "Berlin", in the

"Customers" table:

Trang 57

Combining AND & OR

You can also combine AND and OR (use parenthesis

to form complex expressions)

Example

SELECT * FROM Customers WHERE Country='Germany'

AND (City='Berlin' OR City='München');

The above SQL statement selects all customers from the country "Germany" AND the city must be equal to

"Berlin" OR "München", in the "Customers" table:

Trang 58

The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set

by one or more columns

The ORDER BY keyword sorts the records in

ascending order by default To sort the records in a

descending order, you can use the DESC keyword

SQL ORDER BY Syntax

SELECT column_name,column_name FROM table_name

ORDER BY column_name,column_name ASC|DESC;

Example:

SELECT * FROM Customers ORDER BY Country;

The above SQL statement selects all customers from the "Customers" table, sorted by the "Country" column:

Trang 59

ORDER BY DESC Example

Example

SELECT * FROM Customers ORDER BY Country DESC;

The above SQL statement selects all customers from the

"Customers" table, sorted DESCENDING by the

"Country" column:

ORDER BY Several Columns Example

Example

SELECT * FROM Customers ORDER BY Country,CustomerName;

The above SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the

"CustomerName" column:

Trang 60

The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new

records in a table

It is possible to write the INSERT INTO statement in two forms

The first form does not specify the column names

where the data will be inserted, only their values:

Trang 61

INSERT INTO Example

Assume we wish to insert a new row in the

"Customers" table

We can use the following SQL statement:

Example

 INSERT INTO Customers (CustomerName, ContactName,

Address, City, PostalCode, Country)

VALUES ('Cardinal','Tom B Erichsen','Skagen

21','Stavanger','4006','Norway');

Trang 62

Insert Data Only in Specified Columns

It is also possible to only insert data in specific

columns

The following SQL statement will insert a new row, but only insert data in the "CustomerName", "City", and

"Country" columns (and the CustomerID field will of

course also be updated automatically):

Example

 INSERT INTO Customers (CustomerName, City, Country)

VALUES ('Cardinal', 'Stavanger', 'Norway');

Trang 63

The SQL UPDATE Statement

The UPDATE statement is used to update existing

Assume we wish to update the customer "Alfreds

Futterkiste" with a new contact person and city

We use the following SQL statement:

UPDATE Customers SET ContactName='Alfred Schmidt',

City='Hamburg‘ WHERE CustomerName='Alfreds Futterkiste';

Trang 65

The SQL DELETE Statement

The DELETE statement is used to delete rows in a

table

SQL DELETE Syntax

DELETE FROM table_name WHERE some_column=some_value;

SQL DELETE Example

Assume we wish to delete the customer "Alfreds

Futterkiste" from the "Customers" table

We use the following SQL statement:

DELETE FROM Customers WHERE CustomerName='Alfreds

Futterkiste' AND ContactName='Maria Anders';

Trang 66

Delete All Data

It is possible to delete all rows in a table without

deleting the table This means that the table structure, attributes, and indexes will be intact:

DELETE FROM table_name;

or

DELETE * FROM table_name;

Note: Be very careful when deleting records You cannot

undo this statement!

Trang 67

End ASP.NET Part III Introduction to SQL

Thank You

Trang 68

Visual Studio Express 2012/2010

Visual Studio Express is a free version of Microsoft

Visual Studio

Visual Studio Express is a development tool tailor

made for Web Forms (and MVC)

Visual Studio Express contains:

◦MVC and Web Forms

◦Drag-and-drop web controls and web components

◦A web server language (Razor using VB or C#)

◦A web server (IIS Express)

◦A database server (SQL Server Compact)

◦A full web development framework (ASP.NET)

◦Vista or XP)

Ngày đăng: 18/01/2020, 16:18

TỪ KHÓA LIÊN QUAN