You are on page 1of 15

)DAL(Data Access Layer

DB
Northwind SQL-Server 2005 .....
Server Explorer Add New Connection Server
Name Select Or enter database name
Northwind Test connection
.

)DAL(data access layer



presentation . Presentation ASP.NET
. DAL presentation
:
-1 10 DB 10
DB
-2 .


::
Presentation DAL .
Data Source

Insert,Update .... Delete DAL DAL
DB .
Products Categories DAL
) (
GetCategories()-1 .
GetProducts()-2 .
GetProductByCategorieID(categorieID)-3
DB Query
. ) = type(
. DataSet DataReader.
strongly-typed objects
.

: strongly-typed objects

: loosely-typed object
.
DataReader DataSet loosely-typed object
loosely-typed DataTable
Syntax
]"DataTable.Rows[index]["columnName
strongly-typed DataTable Syntax

DataTable.Rows[index].columnName

strongly-typed object

Typed DataSet .
DAL .

typed DataSet
Typed DataSet Visual studio
DataSet VS
2005 DataSet
. typed
DataSet DataTable,DataRow
TableAdapter
Adapter
.
Typed DataSet strongly -typed collection of data )
( . strongly-typed DataTable
strongly-type DataRow .
strongly-typed DataTable
. products.
strongly-typed DataTable :
) strongly-typed
DataTable products
strongly-typed DataTable( .
dataTable TableAdapter .
TableAdapter products DataTable

)(GetProducts
)GetProductByCategoryID(categoryID
presentation

Typed DataSet Table Adapter


DAL Typed DataSet
Add New Item
DataSet Northwind add
. DataSet App_code
.
TableAdapter Configuration Wizard
combo Box

Next
Connection String
Web.Config ) (
Next
database
). (2

Next

Query Query Bulder


Products add close
) *
( Ok )(3
Advance Option
Next
VS2003
TableAdapter GetData
GetAllProducts .

App_Code/Northwind.xsd products .
DAL

typed-DataSet

ASP.NET GridView Page_Load
;using NorthwindTableAdapters

Page_Load
;)(ProductsTableAdapter productsAdapter = new ProductsTableAdapter

;)(GridView1.DataSource = productsAdapter.GetAllProducts
;)(GridView1.DataBind

!!! GridView .
ADO.NET
DataBase . DropDownList
CheckBox .

DAL
DAL DAL
. productsTableAdapter
Product ID
.
:
Northwind.xsd App_Code
Product ProductTableAdapter

Add Query

:
DataBase
Ad hoc Sql .

:
.
. SQL
CategoryID = @CategoryID : Where
. .@ CategoryID
Next
:
VS . Finish GetProductByProductID(productID)
.
:
Preview Data. Northwind.xsd
Preview


HTML
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Beverages</h1>
<p>
<asp:GridView ID="GridView1" runat="server"
CssClass="DataWebControlStyle">
<HeaderStyle CssClass="HeaderStyle" />
<AlternatingRowStyle CssClass="AlternatingRowStyle" />
</asp:GridView>
&nbsp;</p>
</div>
</form>
</body>

:Code Behind
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NorthwindTableAdapters;

public partial class Beverages : System.Web.UI.Page


{
)protected void Page_Load(object sender, EventArgs e
{
ProductsTableAdapter productsAdapter = new
;)(ProductsTableAdapter
= GridView1.DataSource
;)productsAdapter.GetProductsByCategoryID(1
;)(GridView1.DataBind
}
}


.

Insetr,Update,Delete
. ) integer, string ,
(DataTime .
products Delete productsID
Insert ProductsName String
unitPrice Decimal .... ) (1
10
. .
batch update .
dataset datatable DataRow .
SQL INSERT, UPDATE
DELETE ).(2
TableAdapter Batch Update .
Generate Insert , Update and Delete statement Advance Property
) ( ProductsAdapter )( Update batch
update . GenerateDBDirectsMethod
TableAdapter Update , Insert Delete.
) InsertCommand (property
DeleteCommand TableAdapter UpdateCommand
.
DataSet
DataSet Designer property
. .



NorthwindTableAdapters.ProductsTableAdapter productsAdapter =
new NorthwindTableAdapters.ProductsTableAdapter();
Northwind.ProductsDataTable products = productsAdapter.GetProducts();
foreach (Northwind.ProductsRow product in products)
if (!product.Discontinued && product.UnitsInStock <= 25)
product.UnitPrice *= 2;
// Update the products
productsAdapter.Update(products);

NorthwindTableAdapters.ProductsTableAdapter productsAdapter =
new NorthwindTableAdapters.ProductsTableAdapter();
// Delete the product with ProductID 3
productsAdapter.Delete(3);
// Update Chai (ProductID of 1), setting the UnitsOnOrder to 15
productsAdapter.Update("Chai", 1, 1, "10 boxes x 20 bags",
18.0m, 39, 15, 10, false, 1);
// Add a new product
productsAdapter.Insert("New Product", 1, 1,
"12 tins per carton", 14.95m, 15, 0, 10, false);

Insert,Update,Delete
Delete UpdateInsert DAL
. Insert

.
. Insert
.
Add Query Northwind.xsd
.
Next insert Choose a Query type

VS
);(
Select SCOP_IDENTITY
. Next
. Finish InsetProduct

Property DataSet Designer


. Scalar ExecuteMode
Query Insert
.
:
ProductsTableAdapter adap = new ProductsTableAdapter();
int ID = Convert.ToInt32(adap.Insert("New Product", 1, 1, "12 tins
per carton", 14.95m, 10, 0, 10, false));


: DAL

ProductsTableAdapter
o GetProducts:
SELECT ProductID, ProductName, SupplierID, CategoryID,
QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
ReorderLevel, Discontinued , (SELECT CategoryName FROM
Categories WHERE Categories.CategoryID =
Products.ProductID) as CategoryName, (SELECT CompanyName
FROM Suppliers WHERE Suppliers.SupplierID =
Products.SupplierID) as SupplierName
FROM Products

o GetProductsByCategoryID:
SELECT ProductID, ProductName, SupplierID, CategoryID,
QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
ReorderLevel, Discontinued , (SELECT CategoryName FROM
Categories WHERE Categories.CategoryID =
Products.ProductID) as CategoryName,
(SELECT CompanyName FROM Suppliers WHERE
Suppliers.SupplierID = Products.SupplierID) as SupplierName
FROM Products
WHERE CategoryID = @CategoryID

o GetProductsBySupplierID
SELECT ProductID, ProductName, SupplierID, CategoryID,
QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
ReorderLevel, Discontinued ,
(SELECT CategoryName FROM Categories WHERE
Categories.CategoryID = Products.ProductID)
as CategoryName, (SELECT CompanyName FROM Suppliers
WHERE Suppliers.SupplierID = Products.SupplierID)
as SupplierName
FROM Products
WHERE SupplierID = @SupplierID

o GetProductByProductID
SELECT ProductID, ProductName, SupplierID, CategoryID,
QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder,
ReorderLevel, Discontinued , (SELECT CategoryName
FROM Categories WHERE Categories.CategoryID =
Products.ProductID) as CategoryName,
(SELECT CompanyName FROM Suppliers
WHERE Suppliers.SupplierID = Products.SupplierID)
as SupplierName
FROM Products
WHERE ProductID = @ProductID

CategoriesTableAdapter
o GetCategories
SELECT CategoryID, CategoryName, Description
FROM Categories

o GetCategoryByCategoryID
SELECT CategoryID, CategoryName, Description
FROM Categories
WHERE CategoryID = @CategoryID

SuppliersTableAdapter
o GetSuppliers
SELECT SupplierID, CompanyName, Address, City,
Country, Phone

FROM Suppliers

o GetSuppliersByCountry
SELECT SupplierID, CompanyName, Address,
City, Country, Phone
FROM Suppliers
WHERE Country = @Country

o GetSupplierBySupplierID
SELECT SupplierID, CompanyName, Address,
City, Country, Phone
FROM Suppliers
WHERE SupplierID = @SupplierID

EmployeesTableAdapter
o GetEmployees
SELECT EmployeeID, LastName, FirstName,
Title, HireDate, ReportsTo, Country
FROM Employees

o GetEmployeesByManager
SELECT EmployeeID, LastName, FirstName,
Title, HireDate, ReportsTo, Country
FROM Employees
WHERE ReportsTo = @ManagerID

o GetEmployeeByEmployeeID
SELECT EmployeeID, LastName, FirstName,
Title, HireDate, ReportsTo, Country
FROM Employees
WHERE EmployeeID = @EmployeeID

DAL
Typed-DataSet DataTable TableAdapter
Northwind.xsd Solution Explorer XML
. XML Viewcode
. VB C# Schema
NorthwindTableAdapter ( ctrl+shift+c) Class View

TableAdapter .
ProdouctsTableAdapter Property
GetAllProduct
.
VS

.
Partial Class .
:Partial Class
.
App_Code SuppliersRow.cs

.
;using System
;using System.Data
;using NorthwindTableAdapters
public partial class Northwind
{
public partial class SuppliersRow
{
)(public Northwind.ProductsDataTable GetProducts
{
= ProductsTableAdapter productsAdapter
;)(new ProductsTableAdapter
return
;)productsAdapter.GetProductsBySupplierID(this.SupplierID
}
}
}

. Suppliers SuppliersRow
GetProducts() .
.
Norhwind.SuppliersRow Partial Class
( Class view ). NorthwindTableAdapter

:
SuppliersAndProducts.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="SuppliersAndProducts.aspx.cs"
Inherits="SuppliersAndProducts" %>
<!DOCTYPE html PUBLIC "//
W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.
dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<h1> Suppliers and Their Products</h1>
<p>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
CssClass="DataWebControlStyle">
<HeaderStyle CssClass="HeaderStyle" />
<AlternatingRowStyle CssClass="AlternatingRowStyle" />
<Columns>
<asp:BoundField DataField="CompanyName"
HeaderText="Supplier" />
<asp:TemplateField HeaderText="Products">
<ItemTemplate>
<asp:BulletedList ID="BulletedList1"
runat="server" DataSource='<%#
((Northwind.SuppliersRow)((System.Data.DataRowView)
Container.DataItem).Row).GetProducts() %>'
DataTextField="ProductName">
</asp:BulletedList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
&nbsp;</p>

</div>
</form>
</body>
</html>

SuppliersAndProducts.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NorthwindTableAdapters;
public partial class SuppliersAndProducts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SuppliersTableAdapter suppliersAdapter = new
SuppliersTableAdapter();
GridView1.DataSource = suppliersAdapter.GetSuppliers();
GridView1.DataBind();
}
}