® Object data source truyc pd li uqual ptruyc pd li u.. =mV icác ngdngnh,s d ng SQL data source mang | inhi ul ¡ích, không ph ivi t code C#... Dit ng Data Source - Đóng gói ng d ng - Cá
Trang 1- Gi i tht ucac ki thu t! p trinh ASPNET nang cao
www.poly.edu.vn hoclaptrinhweb.com
Trang 28B -»
=" Cacn idung dah ctrong baitr c
eG ¡thi u các đi u khi nhí nth d lu
Trang 5BBD 0 Ki ntrucbat ng
m" H uh t các chuyên gia phát
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao
hoclaptrinhweb.com
Trang 6"Vid : Dit ngnghi pv : Cacl pbi udi nth cth khach
hang, s nph m, nhan vién
Trang 7Bia hy, ‘ot vriversity
“"C©ungc pph ngth ctruy xutd li ut CSDL
thu t truy c pCSDLnh ADO.NET
= Nén d t trong folder App Code
D it ng Data Source - Dong goi ng d ng - Các kĩthu tl p trình ASPNET nâng cao 7
hoclaptrinhweb.com
Trang 9® Object data source truyc pd li uqual ptruyc pd
li u SQL data source truy c p CSDLtr cti p
=mV icác ngdngnh,s d ng SQL data source mang
| inhi ul ¡ích, không ph ivi t code C#
“mV icác ngd ngÌ n, nên thi tk theo ki ntruc ba
t ng Do do, néns d ng ObjectDataSource
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao
hoclaptrinhweb.com
Trang 10D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 10
hoclaptrinhweb.com
Trang 11BGG Thi tk
=" T ch c ngd ng trên theo ki n trúc bat ng,s d ng
di u khi n ObjectDataSource d thao tác v ¡ CSDL
® [ ng trình bày: Web Form Default.aspx
# Hai đi u khi n objectDatasource
"Hai di ukhi n bu cd_ l¡ u (drop-down list và datalist)
® [ ng trung gian: L p truy c p CSDL ProductDB, | p nghi p
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao
CSDL
hoclaptrinhweb.com
Trang 12D it ng Data Source - Dong goi ng d ng - Các kĩthu tl p trình ASPNET nâng cao
hoclaptrinhweb.com
12
Trang 13= Them ObjectDataSource, ch dnh CategoryDB lal ptruyc p
d li u cho ObjectDataSource nay
=" Lién k t ObjectDataSource v i cac di u khi n
® ihêm ch cnăng c pnh td li uchodi ukhin
® ki m soát | ¡ thao tác v ¡ CSDL
“Vị tmãx lýs ki n cho cács ki n ltemlnserted
hoclaptrinhweb.com
Trang 14<add name="HalloweenConnectionString" connectionString="Data
Source=localhost\sqlexpress;Initial Catalog=Halloween; Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
D it ng Data Source - Đóng gói ng d ng - Các kí thu tl p trình ASPNET nâng cao 14
hoclaptrinhweb.com
Trang 15Dit ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao
hoclaptrinhweb.com
15
Trang 168B -»
= Khai bao cacthu ctinht ng ngv icácc tÌ yt
CSDL va cac ham get, set c acacthu ctinh nay
public class Category
{
private string categoryID;
private string shortName;
private string longName;
public string CategoryID
{
get { return categoryID; }
set { categoryID = value; }
public string ShortName
get { return longName; }
set { longName = value; }
}
public Category()
D ii ng Data Source - Dong gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 16
hoclaptrinhweb.com
Trang 17a8 , FptUniversity
“ Cungc pcácph ngth ctruy xu t, thêm, c pnh t,
xoad li uvatr v ki ud it ng DataOblect cho đi u khi n ObjectDataSource
®D ch dnh CategoryDB la! p truy c p CSDL
=Thém dong [DataObject(true)] vaotr cddng khai bao
Ip
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 17
hoclaptrinhweb.com
Trang 18FPT POLYTECHNIC L p truy c p CSDL CategoryDB
=" Cacph ngth cc aCategoryDB
®*Lychuiktnid = cl u trong file web.config
private static string GetConnectionString()
Trang 19FPT POLYTECHNIC L p truy c p CSDL CategoryDB
Trang 20List<Category> categoryList = new List<Category>();
SqlConnection con = new SqlConnection(GetConnectionString());
string sel = "SELECT CategoryID, ShortName, LongName "
+ “FROM Categories ORDER BY ShortName”" ;
SqlCommand cmd = new SqlCommand(sel, con);
con.Open();
SqlDataReader dr = cmd ExecuteReader (CommandBehavior C1oseConnection);
Trang 21SqlConnection con = new SqlConnection(GetConnectionString());
string ins = "INSERT INTO Categories "
+ " (CategoryID, ShortName, LongName) "
+ " VALUES(@CategoryID, @ShortName, @LongName)";
SqlCommand cmd = new SqlCommand(ins, con);
cmd.Parameters.AddwithValue("CategoryID", category.CategoryID) ; cmd.Parameters.AddwWithValue("ShortName", category.ShortName) ; cmd.Parameters.AddwWwithValue("LongName", category.LongName) ; con.Open();
Trang 22SqlConnection con = new SqlConnection(GetConnectionString());
string up = "UPDATE Categories "
+ "SET ShortName = @ShortName, "
Trang 23SqlConnection con = new SqlConnection(GetConnectionString());
string del = "DELETE FROM Categories "
+ "WHERE CategoryID = @CategoryID "
+ "AND ShortName = @ShortName "
+ "AND LongName = @LongName ";
SqlCommand cmd = new SqlCommand(del, con);
cmd.Parameters.AddwithValue("CategoryID", category.CategoryID) ; cmd.Parameters.AddwWithValue("ShortName", category.ShortName) ; cmd.Parameters.AddwithValue("LongName", category.LongName) ; con.Open();
Trang 24
c p CSDL trong cac tab
SELECT, INSERT Previene | Nest» } | Tinh | | Carel |
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 24
hoclaptrinhweb.com
Trang 25Add New Column
Enable Paging
Enable Editing Enable Deleting Enable Selection
D it ng Data Source - Dong goi ng d ng - Các kĩthu tl p trình ASPNET nâng cao 25
hoclaptrinhweb.com
Trang 26ago Fpt University
FPT POLYTECHNIC Ma aspx c a Object data source
<asp:ObjectDataSource ID="ObjectDataSourcel" runat="server"
Trang 278B ‹ Them ch cnang PPE POLYTECHNIC c pnh td li uchodi ukhi n
= Thém cac button edit, delete cho GridView
= Thém button Insert cho Details
=m) tch d m cdnhcho DetailsView la Insert (gan
thu c tinh DefaultMode = Insert)
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 27
hoclaptrinhweb.com
Trang 28a8 ir Fpt University
m ihêm s ki n RowUpdated, RowDeleted cho GridView
= Thems_ ki nItemlnserted cho DetailsView
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao
hoclaptrinhweb.com
28
Trang 298b Fpt University
protected void GridView1_RowUpdated(
Object sender, GridViewUpdatedEventArgs e)
= "A database error has occurred.<br /><br />" +
= "Another user may have updated that category.’
+ "<br />Please try again ;
Trang 308b Fpt University
protected void GridView1_RowDeleted(
object sender, GridViewDeletedEventArgs e)
+ e.Exception.InnerException.Message;
e.ExceptionHandled = true;
}
else if (e.AffectedRows == @) lblError.Text = "Another user may have updated that category "
+ "<br />Please try again ;
}
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 30
hoclaptrinhweb.com
Trang 318b Fpt University
rrrrorrrrcmicPh ngth cx lýs ki nlteml nserted
protected void DetailsView1_ TtemTnserted(
object sender, DetailsViewInsertedEventArgs e)
+ e.Exception.InnerException.Message;
e.ExceptionHandled = true;
} }
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao
hoclaptrinhweb.com
31
Trang 33® [ri n khai Xcopy
® [ri n khai t¡ n biên dch
® [ o Web Setup Project
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao
hoclaptrinhweb.com
33
Trang 34GD viv
= Copy toanb Filengu nc a ngd ngt | Server
= Hai cach tri n khai Xcopy
®S dng! nh Xcopy
®S dng! nh Copy Web site trong Visual Studio
=" Ph ng pháp này th chi nkhad
m[ih ngdungd † ob ncopyc a ngd ng trên các
Trang 35GD viv Demo
=" M website trong visual studio
= Ch n Copy Web Site trong menu Website
= Click vao Button Connectd m h ptho i Open Web
site d ch nv tricopy filed n
=» Ch ncac File mu ncopy vas d ng button -> d copy
file
` penne [aeons — mann arene F ế | cù Local Internet Information Server
E | Fite Select the Web site you want to opzn ba x
VẬN Senne Wet nile [a] [x] a] Reermenle Wists site: [a F GH Luce! Web Servers
3 Anak AÑ sil S⁄i-lin ShiecflatsSnuersTo< = 4 #Ÿ Dris-lI Vách Site
Trang 368B -» NN llll
=» Biên dch các trang c a ngd ng, sau đó các file
assembly ti n bien dch d c copy t† | server
= Hai cach tri n khai ti n bién dch
®S dng! nh Publish Web Site trong Visual Studio
®S dng! nh aspnet_compilert c as dong! nh
= Tri n khai ti n bién dch conhi u udi mh ntri n khai
Xcopy
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 36
hoclaptrinhweb.com
Trang 37a8 a Fpt University Demo
= Trong Visual Studio Ch n Publish Web Site trong menu
Build
Target Location: (ftp:// , http:// or drive:\path)
g\Documents\Visual Studio 2010\Projects\ ObjectDataSourceT est\PrecompiledWeb\ ObjectDataSource est
Allow this precompiled site to be updatable
N uch ncheckbox, cac
- - - file ngu ns đ ctri n
EF Emit debug information khai cùng các file có th
Use fixed naming and single page assemblies
Enable strong naming on precompiled assemblies th cthi
@ Use a key file generated with the Strong Name tool
Key file location:
Trang 38GD viv
mPh ngphapnayt oram tb cài theo chu n
Windows,s d ngớ cài đt ngd ng Web lênm t
I IS Server
=S d ngkhitri nkhaim t ngd ng trên nhi u server
# Phién b n Visual Web Developer Express khong h tr
ph ng phap tri n khai nay
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 38
hoclaptrinhweb.com
Trang 398B -» Demo
=" Ch n File -> Add -> New Project d hi nth h ptho |
Add New Project
|] Installed Templates » Setup Project Setup and Deployment Type: ype: Setup uP anaweproymen and Deployment
| Visual C# Create a Windows Installer web projectto |
i Other Languages : which files can be added
4 Other Project Types Web Setup Project Setup and Deployment
4 Setup and Deployment InstallShield LE 2 | Merge Module Project Setup and Deployment
Visual Studio Installer
Extensibility | La Setup Wizard Setup and Deployment Database
Modeling Projects a] CAB Project Setup and Deployment Test Projects
Trang 408B - Demo
=" Trongc as_ Solution Explorer, nh nchu t ph i vao Web Setup Project ch n Add -> Project Output
Trang 41
="B cait oran mtrongth m c Debug ho c Realease
c a Web Setup Project, g m 2 file
® Setup.exe: File ch y
“mÐĐ caid t ngd ng Web, Copy file Setup.exe va
Setup.msi t i server vach y file setup.exe
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 41
hoclaptrinhweb.com
Trang 42GD viv
FPTPOLYTECHNIC Ƒ OVà€ uhinhth m c otrong lIIS
mU) Clientcóth truyc pđ n server, các file ng d ng
ph iđ ccopyd nth mcg cc a Web server
(C:\inetoub\wwwroot)
=D Client coth truyc pd nm t website khong n m
trongth m cgc,tat om tth mec otrong lIS ở
tr t ith m cch a các file c a website
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 42
hoclaptrinhweb.com
Trang 43ago Fpt University
D © mì O
FPTPOLYTECHNIC T OVäC€ uhìnhth m c otrong lIS7
=Cacb ct ovac uhinhth mc o
®*T oth moc _ o (Virtual Directory) trong IIS
®*Chuy nth mc othanh ngd ng
®C uhinh xacth c
® T o Application Pool
® Gan Applicaton Pool cho ng d ng
® Kich ho t directory browsing
eThi tl ptrang ch ym cdnh
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 43
hoclaptrinhweb.com
Trang 44a8 , Fpt University
De m O
“m[ oth mec o trong lIS
«®M IIS: Vao Run -> Inetmgr -> OK
® Site -> Default Web Site Nh n chu t ph ich n Add
Connect as
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 44
hoclaptrinhweb.com
Trang 458B -» Demo FPT POLYTECHNIC Tovàc uhìnhth mc o trong IIS7
“m Chuy nth m c othành ng d ng
®Nh n chu tph ivàoth m c o trén IIS GO see ee nes File View Help
nl Add Virtual Directory
Trang 468B -5 Demo
FPT POLYTECHNIC
=» C u hình xácth c
® Ch n Authentication trong vung giao di n chinh
® Thay d itr ng thai Enable/Disable
we 2N — |
c - lø] nñee.=+ 2 my
Sontrets Cniech
mr
; & @ jd a & & A
au ^.:n:zmhc Comensser L+f:.IE -€rtorv - EF'2*212q +inđ:r
Reepan Cse* ng F tra:
Man geet
Contigurate Ets
Trang 47d i ldentity c a Application Pool thanh Local System
Internet Information Services (IS) Manager III NI
CC) 4) >» HAHA >» Application Pools
Ỉ TH '
| | User? True
= - Mai Worcer Processes 1
VirtaulDirectoryTest| | bunne Enabled '
| Tine Mi Rrernase Time TẾ
iny Petivd (ecend Shutecan Tire Li sera 3
uong) | -NET Framework version:
: a B, Add Application Pool | | NET Framework v4.0.30319 |
4-@ Default Web Si) Refresh
Trang 488b Fpt University
D © mì O
FPTPOLYTECHNIC T OVäC€ uhìnhth m c otrong lIS7
= Gan Application Pool cho ng d ng
®Nh n chu tph ¡ vào tênth m c o trong lIS
® Ch n Manage Application -> Advanced Setting
® Ch n Application Pool cho ngd ngt im c Application
Pool trong h p tho | Advanced Setting
|S General
Phys cal Path Credentials
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 48
hoclaptrinhweb.com
Trang 49IL P Logging WI Nodvles utout ecusst = SUL betting
El
£nnfigt:r^*
D it ng Data Source - Đóng gói ng d ng - Các kĩthu tl p trình ASPNET nâng cao 49
hoclaptrinhweb.com
Trang 50& Tavontes SB | Tuggeced Sites +
Bc the most shagedclic gucst at this year's party baby