Download - LINQ to SQL-Tutorial-Can Ban

Transcript
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    1/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    2/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    2

    LINQ to SQL Tutorial

    c dch t blog ScottGu -http://weblogs.asp.net/scottgu/default.aspx

    Ngi dch: o Hi Namhttp://www.daohainam.com

    http://weblogs.asp.net/scottgu/default.aspxhttp://weblogs.asp.net/scottgu/default.aspxhttp://weblogs.asp.net/scottgu/default.aspxhttp://www.daohainam.com/http://www.daohainam.com/http://www.daohainam.com/http://www.daohainam.com/http://weblogs.asp.net/scottgu/default.aspx
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    3/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    3

    Mc lcBi 1: S dng LINQ to SQL .............................................................................................................................. 6

    1. LINQ to SQL l g? .................................................................................................................................. 62. M hnh ha CSDL dng LINQ to SQL: ................................................................................................. 63. Tm hiu lp DataContext ........................................................................................................................ 74. Cc v d LINQ to SQL ........................................................................................................................... 8

    a. Ly cc Product t CSDL .................................................................................................................... 8b. Cp nht mt sn phm trong CSDL ................................................................................................... 9c. Chn thm mt phn loi mi v hai sn phm vo CSDL ................................................................. 9d. Xa cc sn phm .............................................................................................................................. 10e. Gi mt th tc .................................................................................................................................. 11f. Ly cc sn phm v phn trang ........................................................................................................ 12

    5. Tng kt ................................................................................................................................................. 12Bi 2: nh ngha cc lp m hnh d liu ........................................................................................................ 13

    1. To ra mt m hnh d liu LINQ to SQL ............................................................................................. 142. Cc lp thc th ..................................................................................................................................... 153. To cc lp thc th t CSDL ............................................................................................................... 154. Cch t tn v ng php s nhiu......................................................................................................... 175. Quan h gia cc thc th ...................................................................................................................... 186. Delay/Lazy Loading ............................................................................................................................... 197. Dng cc Stored Procedure .................................................................................................................... 19

    Dng SPROCS cp nht/xa,thm d liu ............................................................................................ 218. Tng kt ................................................................................................................................................. 22

    Bi 3: Truy vn Csd liu ............................................................................................................................ 231. M hnh ha CSDL Northwind dng LINQ to SQL.............................................................................. 232. Ly cc sn phm ................................................................................................................................... 233.

    Trc quan ha cc cu truy vn LINQ to SQL trong trnh gli .......................................................... 25

    4. Gn ni cc cu truy vn LINQ to SQL vo cc control LINQ to SQL ................................................ 265. Data Sharping ......................................................................................................................................... 276. Phn trang kt qu truy vn.................................................................................................................... 327. Tng kt ................................................................................................................................................. 34

    Bi 4: Cp nht csd liu ............................................................................................................................. 351. CSDL Northwind c m hnh ha dng LINQ to SQL ..................................................................... 35

    Change Tracking v DataContext.SubmitChanges() ................................................................................. 362. Cc v d Insert v Delete ...................................................................................................................... 37

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    4/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    4

    a. Thm mt sn phm ........................................................................................................................... 38b. Xa cc sn phm .............................................................................................................................. 38

    3. Cp nht thng qua cc quan h............................................................................................................. 384. Transactions ........................................................................................................................................... 405. Kim tra d liu v Business Logic ....................................................................................................... 416. H trkim tra cc gi tr thuc tnh da trn schema ca CSDL ........................................................ 417. H trty bin vic kim tra gi tr cc thuc tnh ................................................................................ 418. H trty bin vic kim tra tnh hp l ca thc th........................................................................... 429. Ty bin cc phng thc kim tra vic thm/xa/sa d liu ............................................................. 4310. Nng cao: Xem danh sch thay i cho Transaction ......................................................................... 4411. Xl cc thay i ng thi vi Optimistic Concurrency: ............................................................... 4512. Dng SPROCs hoc ty bin logic cc cu SQL: .............................................................................. 45

    Bi 5: S dng asp:LinqDataSource .................................................................................................................. 471. ng dng mu m chng ta s xy dng: .............................................................................................. 472. l g v n gip g cho chng ta? ..................................................................... 48

    Bc 1: nh ngha m hnh d liu .......................................................................................................... 49Bc 2: To danh sch sn phm .............................................................................................................. 50Bc 3: B cc ct khng cn thit ........................................................................................................... 54Bc 4: Lc danh sch sn phm .............................................................................................................. 57Bc 5: Thm cc quy tc kim tra logic .................................................................................................. 60

    3. Tng kt ................................................................................................................................................. 63Bi 6: Ly d liu dng Stored Procedure ......................................................................................................... 65

    1. Dng SPROC hay khng SPROC? l mt vn . ..................................................................... 652. Cc bc nh x v gi SPROC dng LINQ to SQL ............................................................................ 663. Cch nh x mt SPROC vo mt DataContext ca LINQ ................................................................... 674. Cch gi SPROC mi c to ............................................................................................................. 685. nh x kiu tr v ca phng thc SPROC vo mt lp trong m hnh d liu ................................ 696. X l cc tham s th tc dng OUTPUT ............................................................................................. 727. X l cc th tc tr v nhiu kiu kt qu khc nhau........................................................................... 73

    H trcc hm do ngi dng tnh ngha (UDF) ................................................................................. 758. Tng kt ................................................................................................................................................. 77

    Bi 7: Cp nht d liu dng Stored Procedure ................................................................................................. 789. Bc 1: To lp truy xut d liu (cha dng n cc th tc) ............................................................ 78

    Thm cc quy tc kim tra d liu vo cc lp m hnh d liu ............................................................... 78Thm phng thc GetCustomer() vo lp DataContext ......................................................................... 80

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    5/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    5

    10. Bc 2: Dng lp truy cp d liu (cha s dng SPROC) .............................................................. 8111. Chmt giy - Ti ngh bi vit ny nh ni v vic dng SPROC cm ??? ............................... 8212. Cch s dng SPROC thc hin Insert/Update/Delete ................................................................. 8313. Bc 3: Thm mt Order bng cch dng SPROC ........................................................................... 8314. Bc 4: Thc hin cp nht dng SPROC ........................................................................................ 8715. Bc 5: Dng lp DAL ln na ......................................................................................................... 8916. Mt su im ca vic dng SPROC ............................................................................................. 89

    Dng cc tham s dng output: .................................................................................................................. 89S th no nu mt SPROC pht ra mt li? ............................................................................................. 90Ti c th vit code thay v dung ORM designer gi SPROC? ............................................................ 90

    17. Tng kt ............................................................................................................................................. 90Bi 8: Thc thi cc biu thc SQL ty bin ....................................................................................................... 91

    1. Dng cc cu truy vn SQL ty bin vi LINQ to SQL ........................................................................ 932. Dng ExecuteQuery ............................................................................................................................... 933. Ty bin cc biu thc SQL v theo vt (tracking) cc thao tc cp nht: ............................................ 944. Ty bin cc biu thc SQL vi cc lp ca bn .................................................................................. 955. Ty bin cc cu SQL cho Inserts/Updates/Deletes .............................................................................. 956. Tng kt ................................................................................................................................................. 96

    Bi 9: Dng biu thc LINQ ty bin vi .................................................................... 971. Tm tt: dng vi mt mnh where c khai bo .................................... 972. Dng cc s kin Selecting vi ........................................................................ 983. Thc hin cc php chiu khi truy vn vi s kin Selecting .............................................................. 1014. Tng kt ............................................................................................................................................... 103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    6/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    6

    Bi 1: S dng LINQ to SQL

    y l bi vit u tin trong lot bi c ch LINQ to SQL, cc bi ny s cho bn mt ci nhnkhi qut, gip bn lm quen vi LINQ, mt trong nhng cng ngh mi c trong .NET 3.5.

    Lot bi ny c da trn lot Tutorial ca ScottGu (http://weblogs.asp.net/scottgu).

    1. LINQ to SQL l g?LINQ to SQL l mt phin bn hin thcha ca O/RM (object relational mapping) c bn trong.NET Framework bn Orcas (nay l .NET 3.5), n cho php bn m hnh ha mt c s d liudng cc lp .NET. Sau bn c th truy vn c s d liu (CSDL) dng LINQ, cng nh cpnht/thm/xa d liu t .

    LINQ to SQL h tr y transaction, view v cc stored procedure (SP). N cng cung cp mt

    cch d dng thm kh nng kim tra tnh hp l ca d liu v cc quy tc vo trong m hnh dliu ca bn.

    2. M hnh ha CSDL dng LINQ to SQL:Visual Studio Orcas tch hp thm mt trnhthit kLINQ to SQL nh mt cng c d dngcho vic m hnh hamt cch trc quan cc CSDL dng LINQ to SQL. Bi vit sau si su hnvo cch dng trnh thit k ny(bn cng c thxem on video ny xem cch ti to mt mhnh LINQ to SQL).

    Bng cch dng trnhthit kLINQ to SQL, ti c th d dng to mt m hnh cho CSDL muNorthwind ging nh di y:

    http://weblogs.asp.net/scottguhttp://weblogs.asp.net/scottguhttp://weblogs.asp.net/scottguhttp://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspxhttp://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspxhttp://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspxhttp://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspxhttp://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspxhttp://weblogs.asp.net/scottgu/archive/2007/01/28/video-using-linq-with-asp-net-in-vs-orcas-part-1.aspxhttp://weblogs.asp.net/scottgu
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    7/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    7

    M hnh LINQ to SQL trn nh ngha bn lp thc th: Product, Category, Orderv OrderDetail.Cc thuc tnh ca mi lp nh x vo cc ct ca bng tng ng trong CSDL. Mi instance camt lp biu din mt dng trong bng d liu.

    Cc mi tn gia bn lp thc th trn biu din quan h gia cc thc th khc nhau, chng cto ra da trn cc mi quan h primary-key/foreign-key trong CSDL. Hng ca mi tn ch ra miquan h l mt mt hay mt nhiu. Cc thuc tnh tng ng s c thm vo cc lp thc thtrong cc trng hp ny. Ly v d, lp Category trn c mt mi quan h mt nhiu vi lpProduct, iu ny c ngha n s c mt thuc tnh Categories l mt tp hp cc i tngProduct trong Category ny. Lp Product cng s c mt thuc tnh Category ch n itngCategory cha Product ny bn trong.

    Bngcc phng thcbn tay phibn trong trnh thit k LINQ to SQL trn cha mt danh schcc SP tng tc vi m hnh d liu ca chng ta. Trong v d trn ti thm mt th tc c tn

    GetProductsByCategory. N nhn vo mt categoryID v tr v mt chui cc Product. Chng tas xem bng cch no c th gi c th tc ny trong mt on code bn di.

    3. Tm hiu lp DataContextKhi bn bm nt Save bn trong mn hnh thit k LINQ to SQL, Visual Studio s lu cc lp.NET biu din cc thc th v quan h bn trong CSDL m chng ta va m hnh ha. C mi mtfile LINQ to SQL chng ta thm vo solution, mt lp DataContext s c to ra, n s c dngkhi cn truy vn hay cp nht li cc thay i. Lp DataContext c to s c cc thuc tnh biudin mi bng c m hnh ha t CSDL, cng nh cc phng thc cho mi SP m chng ta thm vo.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    8/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    8

    Ly v d, di y l lp NorthwindDataContext c sinh ra da trn m hnh chng ta to ra trn:

    4. Cc v d LINQ to SQLMt khi m hnh ha CSDL dng trnh thit k LINQ to SQL, chng ta c th d dng vit ccon lnh lm vic vi n. Di y l mtvi v d v cc thao tc chung khi x l d liu:

    a. Ly cc Product tCSDL

    on lnh di y dng c php LINQ ly v mt tp IEnumerable cc i tng Product. Ccsn phm c ly ra phi thuc phn loi Beverages:

    C#:

    VB:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    9/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    9

    b. Cp nht mt sn phm trong CSDLon lnh di y cho thy cch ly mt sn phm, cp nht li gi tin v lu li CSDL.

    C#:

    VB:

    c. Chn thm mt phn loi mi v hai sn phm vo CSDLon m di y biu din cch to mt phn loi mi, v to hai sn phm mi v a chng votrong phn loi to. C ba sau s c a vo c s d liu.

    Ch rng ti khng cn phi t qun l cc mi quan h primary key/foreign key, thay vo , tich n gin thm cc i tng Product vo tp hp Products ca i tng category, v ri thmi tng category vo tp hp Categories ca DataContext, LINQ to SQL s bit cch thit lp ccgi tr primary key/foreign key mt cch thch hp.

    (Add c thay i bng InsertOnSubmit trong phin bn hin ti)

    C#

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    10/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    10

    VB:

    d. Xa cc sn phmon m sau s biu din cch xa tt c cc sn phm Toy khi CSDL:

    (RemoveAll c thay i bng DeleteAllOnSubmit trong phin bn hin ti)

    C#:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    11/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    11

    VB:

    e. Gi mt th tcon m di y biu din cch ly cc thc th Product m khng dng c php ca LINQ, mgi n th tc GetProductsByCategory chng ta thm vo trc y. Nh rng mt khi ly

    v kt qu, ti c th cp nht/xa v sau gi db.SubmitChanges() cp nht cc thay i tr liCSDL.

    C#:

    VB:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    12/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    12

    f. Ly cc sn phm v phn trangon m di y biu din cch phn trang trn server nh mt phn ca cu truy vn LINQ. Bngcch dng cc ton t Skip() v Take(), chng ta s ch tr v 10 dng t CSDL bt u t dng200.

    C#:

    VB:

    5. Tng ktLINQ to SQL cung cp mt cch hay, r rng m hnh ha lp d liu trong ng dng ca bn.

    Mt khi nh ngha m hinh d liu, bn c th d dng thc hin cc cu truy vn cng nh cpnht, xa, sa d liu mt cch hiu qu.

    Hi vng nhng hng dn v v d mutrn gip bn lm quen vi LINQ. Ti s tip tc ccbi vit ny gip bn khm ph LINQ to SQL mt cch chi tit hn.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    13/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    13

    Bi 2: nh ngha cc lp m hnh d liu

    Trongphn mt, ti tho lun v LINQ to SQL l g v cung cp mt ci nhn c bn v nhngtrng hp chng ta c th s dng n.

    Trong bi vit u tin, ti cng cung cp cc on code mu biu din cch x l d liu dngLINQ to SQL, bao gm:

    Cch truy vn d liu Cc cp nht d liu Cch chn v to quan h cc dng trong mt CSDL Cch xa cc dng trong mt CSDL Cch gi mt th tc Cch ly d liu v phn trang trn server

    Ti thc hin tt c cc thao tc d liu bng cch dng mt m hnh d liu LINQ to SQLging nh di y:

    Trong bi ny, ti s i vo chi tit cch to ra mt m hnh d liu LINQ to SQL ging nh trn.

    LINQ to SQL, cng nh LINQ to SQL, v tt c cc tnh nng khc m ti ni n trong lotbi ny s c coi nh mt phn ca .NET 3.5 v Visual Studio Orcas (nay l Visual Studio2008).

    Bn c th lm theo tt c cc bc di y bng cch ti v hocVisual Studio 2008hocVisualWeb Developer Express. C hai u c th c ci t v dng ng thi vi Visual Studio 2005.

    http://msdn.microsoft.com/en-us/vstudio/products/aa700831.aspxhttp://msdn.microsoft.com/en-us/vstudio/products/aa700831.aspxhttp://msdn.microsoft.com/en-us/vstudio/products/aa700831.aspxhttp://www.microsoft.com/express/vwd/http://www.microsoft.com/express/vwd/http://www.microsoft.com/express/vwd/http://www.microsoft.com/express/vwd/http://www.microsoft.com/express/vwd/http://www.microsoft.com/express/vwd/http://msdn.microsoft.com/en-us/vstudio/products/aa700831.aspx
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    14/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    15/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    15

    2. Cc lp thc thLINQ to SQL cho php bn m hnh ha cc lp nh x vo CSDL. Cc lp ny thng c lEntity Class (lp thc th) v cc instance ca n thng c gi l Entity (thc th). Cc lp

    entity nh x vo cc bng bn trong mt CSDL. Cc thuc tnh ca cc lp thng thng nh xvo cc ct trong bng. Mi instance ca mt lp thc th biu din mt dng trong bng.

    Cc lp thc th trong LINQ to SQL khng cnphi k tha t mt lp c bit no khc, iu cho php bn c th cho php chng tha k t bt c i tng no bn mun. Tt c cc lp cto ra dng LINQ to SQL designer u c nh ngha nh partial class c ngha l bn c thvit thm code thm vo cc thuc tnh, phng thc v s kin cho chng.

    Khng ging nh chc nng DataSet/TableAdapter c trong VS 2005, khi dng LINQ to SQLdesigner, bn khng cn ch ra cu truy vn SQL c dng to ra m hnh v lp truy xut dliu.

    Thay vo , bn tp trung ch yu vo vicnh ngha cc lp thc th, cch chngnh x voCSDL, v mi quan hgia chng. Trnh LINQ to SQL c th m bn dng s m bo vic sinh racc lnh SQL thch hp vo lc chy khi bn tng tc v lm vic vi cc thc th d liu. Bn cth dng c php truy vn LINQ ch ra cch bn mun truy vn d liu.

    3. To cc lp thc th tCSDLNu ccu trccho CSDL, bn c th dng n to cc lp thc th LINQ to SQL mt cchnhanh chng.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    16/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    16

    Cc d dng nht lm iu ny l m CSDL trong ca s Server Explorer bn trong VisualStudio, chn cc table v view m bnmun m hnh ha, v ko th chng ln trn ca s LINQ toSQL designer.

    Khi bn thm 2 bng (Categories and Products) v 1 view (Invoices) t CSDL Northwind vo cas LINQ to SQL designer, bn s c thm 3 lp thc th c to ra mt cch t ng:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    17/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    17

    Dng cc lp m hnh ha d liu trn, bn c th chy tt c ccon lnh muc mt t trongphn 1ca lot bi ny. Ti khng cn thm bt k on code no hay cu hnh c th thc hinc cc thao tc query, insert, update, delete v phn trang.

    4. Cch t tn v ngphp s nhiuMt trong nhng th bn nghe nhc n khi dung LINQ to SQL l n c th t ng chuyn tn

    bng v ct thnh dng s nhiu khi to cc lp thc th. Ly v d: Bng Products trong v d cachng ta to ra lp Product, cng nh bng Categories to ra lp Category. Cch t tn nygip m hnh ca bn thng nht vi quy c t tn trong .NET.

    Nu khng thch tn lp hay tn thuc tnh do trnh designer sinh ra, bn vn c th sa li thnh btc tn no bn thch. Bn c th lm iu ny bng cch chnh sa tn thc th/thuc tnh bn trongtrnh thit k hoc thng qua bng thuc tnh.

    Kh nng t tn cho cc thc th/thuc tnh/quan h khc vi tn trong CSDL rt hu dng trong

    mt s trng hp, v d:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    18/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    18

    1. Khi tn bng/ct trong CSDL b thay i. Bi v m hnh thc th ca bn c th c tn khc vitn trong CSDL, do vy bn c th ch cn cp nht li cc quy tc nh x m khng cn cp nhtchng trnh hoc cc lnh truy vn c th dng c tn mi.

    2. Khi cc thnh phn bn trong CSDL c t tn khng r rng. V d: thay v dngau_lnamev au_fnamecho cc tn thuc tnh ca mt lp thc th, bn c th t tn chngthnh LastName v FirstName trong lp thc th v vit cc lnh dng vi n (m khng cni tn cc ct trong CSDL).

    5. Quan h gia cc thc thKhi bn ko th cc i tng t Server Explorer ln trn ca s LINQ to SQL designer, VS s tng xc nh cc mi quan h primary key/foreign key gia cc i tng, v t ng to cc quanh mc nhin gia cc lp thcth khc nhau m n to. V d, khi bn thm c hai bngProducts v Categories t Northwind ln trn ca s LINQ to SQL, bn c th thy mi mi quan h

    mt nhiu gia chng (c biu din bngmt mi tn trn ca s son tho):

    Mi quan h trn s lm lp thc th Product c thm mt thuc tnh l Category, bn c th dng truy cp vo thc th Category ca mt Product. N cng lm lp Category c thm thuc tnhProducts, y l mt tp hp cho php bn ly ra tt c cc Product c trong Category .

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    19/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    20/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    20

    Ti c th dng Server Explorer trong VS ko/th th tc SPROC ln trn ca s son tho LINQto SQL c th thm mt phng thc cho php goi SPROC. Nu ti th SPROC ln trn thc thProduct, LINQ to SQL designer s khai bo SPROC tr v mt tp kt qu c kiuIEnumerable:

    Sau ti c th dng c php LINQ to SQL hay gi thng phng thc trn ly v cc thcth t CSDL:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    21/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    21

    Dng SPROCS cp nht/xa,thm dliu

    Mc nhin LINQ to SQL s t ng to ra cc biu thc SQL ph hp cho bn mi khi mun cpnht/xa/thm d liu. V d, nu bn vit m LINQ to SQL nh di y cp nht mt s gi trtrn mt thc th Product:

    Mc nhin, LINQ to SQL s to v thc thi pht biu UPDATE tng ng khi bn xc nhn thay i(ti s ni thm v vn ny trong nhng bi vit khc).

    Bn cng c th nh ngha v dng cc th tc INSERT, UPDATE, DELETE nu mun. cuhnh, click ln mt lp thc th trong ca s LINQ to SQL v trong bng thuc tnh, nhn chut lntrn nt trn cc gi tr Delete/Insert/Update, v chn SPROC m bn nh ngha.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    22/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    22

    C mt iu hay l nhng thay i trn hon ton c thc hin lp nhx LINQ to SQL cngha l tt c nhng on lnh m ti vit trc y u c th tip tc lm vic m khng cnthay i bt k iu g. iu ny gip trnh phi thay i li code ngay c nu sau ny bn mundng mt hm SPROC ti u hn sau ny.

    8. Tng ktLINQ to SQL cung cp mt cch thc n gin, sng sa m hnh ha lp d liu trongng dngca bn. Mt khi bnnh ngha m hnhd liu, bn c th thc hin cc cu truy vn, thm, cpnht vxa d liu mt cch d dng v hiu qu.

    Dng trnh thit k LINQ to SQL c sn trong Visual Studio v Visual Web Developer Express, bnc th to v qun l m hnh d liu cc k nhanh. Trnh LINQ to SQL designer cng v cng mmdo bn c th ty bin cc hnh vi mc nhin v ghi hoc m rng h thng sao cho ph hpvi nhng yu cu c th no .

    Trong nhng bi tip theo ti s dng m hnh d liu chng ta to ra trong bi ny o sauhn vo vic truy vn, thm, cp nht v xa d liu. Trong cc bi vit v cp nht, thm, xa ticng s tho lun v cch thm cc on lnh kim tra d liu cng nh cc quy tc vo cc lpthc th chng ta nh ngha trn.

    Mike Taulty cng c mt son videort hay v LINQ to SQL m bn nn xemti y. Chngcung cp mt cch tuyt vi hc bng cch xem nhng ngi khc tng bc s dng LINQ toSQL.

    http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/05/10/9322.aspxhttp://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/05/10/9322.aspxhttp://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/05/10/9322.aspxhttp://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/05/10/9322.aspx
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    23/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    24/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    24

    Trong cu truy vn trn, ti dng mt mnh where trong c php LINQ ch tr v cc sn

    phm trong mt category cho trc. Ti hin ang dng CategoryID ca Product thc hin lc racc dng mong mun.

    Mt trong nhng im hay l ti c rt nhiu la chn, rt nhiu cch ty bin cu lnh, v ti cth nm bt u im ca mi quan h gia cc thc th m ti to khi m hnh ha cc lp lmcho cu lnh phong ph v t nhin hn. V d, ti c th sa li cu truy vn lc ra cc dng theoCategoryName thay v CategoryID bng cch vit cu lnh LINQ nh sau:

    Ch cch ti dng thuc tnh Category trn mi i tng Product lc theo CategoryNameca Category cha Product . Thuc tnh ny c t ng to ra bi LINQ to SQL v chng ta m hnh ha cc lp Category v Product nh mt mi quan h mt-nhiu.

    Mt v d khc v cch dng quan h trong m hnh d liu bn trong cc cu truy vn, chng ta cth vit cu lnh LINQ nh di y ly v ch nhng Product c 5 hoc hn n t hng:

    Ch cch chng ta dng tp hp OrderDetails m LINQ to SQL to trn mi lp Product(nh vo mi quan h mt-nhiu m chng ta m hnh ha trong trnh thit k LINQ to SQL).

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    25/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    25

    3. Trc quan ha cc cu truy vn LINQ to SQL trong trnh gliCc trnh nh x O/R (Object relational mapper) nh LINQ to SQL to ra v thc thi cc cu lnhSQL mt cch t ng mi khi bn thc hin mt cu truy vn hay cp nht m hnh i tng ca

    n.Mt trong nhng iu quan tm ln nht m cc lp trnh vin mi quen vi ORM l: Cu lnhSQL thc s c thc thi l g?. Mt iu thc s th v v LINQ to SQL l n cho php xem rtd dng cu lnh SQL c thc thi thc s khi bn chyng dng trong chg li.

    Bt u t bn Beta2 ca VS 2008, bn c th dng mt LINQ to SQL visualizer plug-in xem mtcch d dng (v kim tra) bt k cu lnh truy vn LINQ to SQL no. Ch cn t mt breakpoint vdi chut ln trn mt cu lnh LINQ to SQL, sau nhn vo biu tng chic knh lp xem gitr ca cu lnh mt cch trc quan:

    Mt ca s s hin ln cho php bnxem mt cch chnh xc cu lnh LINQ to SQL m LINQ toSQL s dng ly v cc i tng Product:

    Nu bn nhn nt Execute trn ca s ny, n s cho php bn chy cu lnh SQL trc tip trongtrnh debugger v xem mt cch chnh xc d liu c tr v:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    26/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    26

    iu ny r rng lm cho vic xem nhng g LINQ to SQL lm cho bn tr thnh cc k d dng.Nh rng bn c th d dng thay th cu SQL m LINQ to SQL thc thi nu mun - mc d trong98% trng hp ti ngh bn s thy rngcu lnh m LINQ to SQL thc thi l thc s, thc s tt.

    4. Gn ni cc cu truy vn LINQ to SQL vo cc control LINQ to SQLCc cu truy vn LINQ tr v kt qu m n s implement interrface IEnumerable y cng linterface m cc control ASP.NET dng h tr gn ni cc i tng. iu ny c ngha l bn cth gn ni kt qu ca bt k cu lnh LINQ, LINQ to SQL hay LINQ to XML vo bt k controlASP.NET no.

    Ly v d, bn c th khai bo mt control trong mt trang .aspx ging nh sau:

    Ti cng c th gn ni kt qu ca cu LINQ to SQL vit trc y vo GridView ging nhsau:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    27/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    27

    N s sinh ra mt trang trng nh sau:

    5. Data SharpingHin ti, mi khixc nh kt qu truyvn, chng ta ly ton b cc ct d liu cn thit cho cc itng thuclp Product:

    V d, cu truy vn sau ly v cc sn phm:

    V ton b kt qu c tr v:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    28/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    29/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    29

    Mt iu thc s th v v LINQ to SQL l ti c th tn dng tt c u im ca cc quan h trongm hnh d liu khi mun gt gia li d liu. N cho php ti biu din y v hiu qu cc cutruy vn. Ly v d, cu truy vn di y ly v ID v Name t thc th Product, tng s n hng c t cho sn phm , v ri ly tng gi tr ca tng n hng:

    LINQ to SQL thng minh c thchuyn biu thc LINQ trn thnh cu SQL di y khin c thc thi:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    30/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    30

    Cu SQL trn cho php tnh ton tt c cc gi tr ca NumOrders v Revenue t ngay trn SQLserver, v tr v ch nhng d liu nh di y (lm cho vic thc thi c nhanh chng):

    Chng ta c th gn ni tp kt qu vo control GridView to ra mt giao din p hn:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    31/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    31

    Bn cng c th c h tr y bi tnh nng intellisense bn trong VS 2008 khi vit cc cutruy vn LINQ:

    Trong v d trn, ti ang s dng mt kiu v danh (anonymous type) v dngobject initialization gt gia v nh ngha cu trc tr v. Mt iu thc s tuyt vi l VS 2008 cung cp intellisensey , kim tra lc dch v c refactoring khi lm vic c vi cc tp kt qu c kiu v danh:

    http://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=truehttp://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=truehttp://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=truehttp://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspxhttp://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=true
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    32/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    32

    6. Phn trang kt qu truy vnMt trong nhng yu cu chung khi vit cc trang web l bn phi c kh nng phn trang mt cc hhiu qu. LINQ cung cp sn hai hm m rng cho php bn c th lm iu mt cch d dng vhiu qu hm Skip() v Take().

    Bn c th dng Skip() v Take() nh di y ch ra rng bn ch mun ly v 10 i tng snphm bt u t mt sn phm cho trc m chng ta chi ra trong tham s truyn vo:

    Ch trnti khng dng Skip() v Take() trong cu khai bo truy vn cc sn phmm chdng ti khi gn kt d liu vo GridView. Mi ngi hay hi C phi lm nh vy th cu lnhu tin s ly ton b d liu t CSDL v lp gia, ri sau mi thc hin vic phn trang ?. Cutr li l Khng. L do l v LINQ ch thc s thc thi cc cu truy vn khi bn ly kt qu t nm thi.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    33/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    33

    Mt trong nhng u im ca m hnh ny l n cho php bn c th vit cc cu lnh phc tp bngnhiu bc, thay v phi vit trong mt cu lnh n (gip d c hn). N cng cho php bn to racc cu truy vn t cc cu khc, gip bn c th xy dng cc cu truy vn rt phc tp cng nh cth dng li c cc cu truy vn khc.

    Mt khi ti c phng thc BindProduct() nh ngha trn, ti c th vit lnh nh di y ly v ch s u t query string, v cho php danh sch sn phm c th c hin phn trang vhin th:

    N s cho chng ta mt trang hin th cc sn phm c nhiu hn 5 n t hng, cng vi doanh thutng ng, v c phn trang da trn tham s truyn vo qua query string:

    Ghi ch: Khi lm vic vi SQL 2005, LINQ to SQL s dng hm ROW_NUMBER() thc hinvic phn trang logic trong CSDL. N m bo rng ch 10 dng d liu c tr v khi chng tathc hin cc cu lnh trn:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    34/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    34

    N lm cho vic phn trang hiu qu v d dng hn, c bit l vi cc tp d liu ln.

    7. Tng ktHi vng cc bc trn cung cp mt ci nhn y v nhng c tnh m LINQ to SQL cungcp, tm hiu thm v cc biu thc LINQ v c php mi c dng trong C# v VB.NET trongVS 2008, xin hy tham kho thm cc bi vit sau:

    Automatic Properties, Object Initializer and Collection Initializers Extension Methods Lambda Expressions Query Syntax Anonymous Types

    Trong bi vit tip theo trong lot bi ny, ti s cho thy cch thm cc php kim tra vo m hnhd liu ca chng ta, v biu din cch chng ta c th dng a logic chng trnh vo mi lnthc thi cc cu lnh update, insert,hay delete d liu. Ti cng s cho cc bn thy cc tnh nngcao cp hn ca lazy loading v eager loading, cch dng control mi htr vic khai bo databinding trong ASP.NET, cch gii quyt xung t

    http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspxhttp://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspxhttp://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspxhttp://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspxhttp://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspxhttp://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=truehttp://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=truehttp://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx?CommentPosted=truehttp://weblogs.asp.net/scottgu/archive/2007/04/21/new-orcas-language-feature-query-syntax.aspxhttp://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspxhttp://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    35/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    35

    Bi 4: Cp nht cs d liu

    Trong bi hm nay, ti s ni r hn v cch chng ta dng CSDL c m hnh ha trc y,v dng n cp nht, chnh sa v xa d liu. Ti cng s cho cc bn thy cc chng ta c th

    thm cc quy tc (business rule sau ny tr i ti s nguyn t business rule, v t ny r nghahn) v ty bin cch xc thc tnh hpl ca d liu.

    1. CSDL Northwind c m hnh ha dng LINQ to SQLTrongphn 2ca lot bi ny, ti i qua cc bc to nn m hnh cc lp LINQ to SQL dngLINQ to SQL designer c trong VS 2008. Di y l s lp c to cho CSDL muNorthwind v cng s l m hnh c dng trong bi vit ny:

    Khi chng ta nh ngha m hnh d liu dng LINQ to SQL designer nh trn, chng ta nhngha ra 5 lp m hnh: Product, Category, Customer, Order and OrderDetail. Cc thuc tnh cami lp nh x vo cc ct tng ng trong bng d liu. Mi i tng thuc lp thc th s biudin mt dng trong bng CSDL.

    Khi nh ngha m hnh d liu, LINQ to SQL designer cng to ra mt lp DataContext cung cpcc cch thc truy vn v cp nht li d liu. Trong m hnh mu chng ta nh ngha trn,

    lp ny c t tn l NorthwindDataContext. Lp NorthwindDataContext c cc thuc tnh biu

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    36/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    37/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    37

    Ghi ch: Vic lu vt LINQ to SQL xy ra bn phachng trnhgi, v khng lin quan g nCSDL. C ngha l bn khng h dng ti nguyn trn CSDL, hoc bn khng cn ci t thm haythay i bt k th g trn CSDL cho php lm iu ny.

    Sau khi cp nht cc i tng chng ta ly t LINQ to SQL, chng ta c th gi phngthcSubmitChanges() trn lp DataContext cp nht li cc thay i ln CSDL. Vic gi

    phng thc ny s lm cho LINQ to SQL tnh ton ng v thc thi cc cu lnh SQL ph hp cp nht CSDL.

    Ly v d, bn c th vit cu lnh di y cp nht li gi tin v s lng n v cn li casn phm Chai:

    Khi ti gi northwind.SubmitChanges() nh trn, LINQ to SQL s xy dng v thc thi mt culnh SQL UPDATE m n s cp nht li hai thuc tnh ca sn phm m chng ta sa li nh trn.

    Ti c th vit on lnh di y duyt qua danh sch cc sn phm t ph bin v gi cao, sau t li thuc tnh ReorderLevel = 0:

    Khi ti gi northwind.SubmitChanges() nh trn, LINQ to SQL s tnh ton v thc thi mt tp thchhp cc pht biu UPDATE cp nht cc sn phm c thuc tnh ReorderLevel b thay i.

    Hy nh l nu gi tr ca cc thuc tnh ca i tng Product khng b thay i bi cu lnh trn,c ngha l bn thn i tng khng b thay i, th LINQto SQL cng s khng thc thi bt k culnh UPDATE no trn i tng . V d, nu n gi ca i tng Chai l 2 v s san

    phm cn li l 4, th vic gi SubmitChanges() s chng lm thc thi bt k cu SQL no. Cngvy, ch cc sn phm trong v d th hai c ReorderLevel khng bng 0 mi c cp nht khi giSubmitChanges().

    2. Cc v d Insert v DeleteNgoi vic cp nht cc dng c trong CSDL, LINQ to SQL cn cho php bn thm v xa dliu. Bn c th lm c iu ny bng vic thm/bt cc i tng d liu t cc tp hp bng

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    38/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    38

    trong lp DataContext, v sau gi SubmitChanges(). LINQ to SQL s lu vt li cc thao tc ny,v t ng thc thi cu lnh SQL INSERT hay DELETE ph hp khi phng thc SubmitChanges()c gi.

    a. Thm mt sn phmBn c th thm mt sn phm mi vo CSDL bng vic to ra mt i tng thuc lp Product,gn cc gi tr thuc tnh, v sau thm n vo tp hp Products ca DataContext:

    Khi gi SubmitChanges nh trn, mt dng mi s c thm vo bng Product.

    b. Xa cc sn phmCng nh ti ni v vic thm mt sn phm mi bng cch i tng Product vo tp hpProducts ca DataContext, ti cng c th lm mt cch ngc li khi mun xa mt sn phm tCSDL bng cch xa n khi tp hp ny:

    (RemoveAll c thay i bng DeleteOnSubmit trong phin bn hin ti)

    Ch cch ti ly mt tp hp cc sn phm khng cn c sn xut v cng khng c n thng no bng cch dng mt cu truy vn LINQ, ri sau truyn n cho phng thc Remove Allca tp hp Products trong DataContext. Khi gi SubmitChanges(), tt c cc sn phm s b xakhi CSDL.

    3. Cp nht thng qua cc quan hiu lm cho cc trnh ORM nh LINQ to SQL cc k mm d l n cho php chng ta d dng mhnh ha mi quan h gia cc bng trong m hnh d liu. V d, ti c th m hnh ha

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    39/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    39

    miProduct trong mt Category, mi Order cha cc OrderDetails cho tng mc, kt hp ccOrderDetail vi mt Product, v lm cho mi Customer kt hp vi mt tp cc Order. Ti biu din cch xy dng v m hnh ha cc mi quan h trongphn 2ca lot bi ny.

    LINQ to SQL cho php ti tn dngc u im ca cc mi quan h trong vic truy vn v cpnht d liu. V d, ti c th vit on lnh di y to mt Product mi v kt hp n vi mtcategory Beverages trong CSDL nhdi y:

    (Add c thay i bng InsertOnSubmit trong phin bn hin ti)

    Hy ch cch ti thm mt i tng Product vo tp hp Products ca mt Category. N s ch rarng c mt mi quan h gia hai i tng, v lm cho LINQ to SQL t ng duy tr mi quan hforeign-key/primary key gia c hai khi ti gi SubmitChanges.

    Mt v d khc cho thy LINQto SQL c th gip qun l quan h gia cc bng nh th no vgip cho vic lp trnh sng sa hn, hy xem mt v d di y khi ti to mt Order mi cho mtkhch hng c. Sau khi t gi tr cho ngy chuyn hng v chi ph cho vic t hng, tis totip 2 mc chi tit trong n t hng ch n cc sn phm m khch hng ang mun mua. Sau, ti s kt hp n t hng vi khch hng, v cp nht cc thay i vo CSDL.

    http://namdh.wordpress.com/2008/11/07/linq-to-sql-part-2-modeling-classes/http://namdh.wordpress.com/2008/11/07/linq-to-sql-part-2-modeling-classes/http://namdh.wordpress.com/2008/11/07/linq-to-sql-part-2-modeling-classes/http://namdh.wordpress.com/2008/11/07/linq-to-sql-part-2-modeling-classes/
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    40/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    41/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    41

    5. Kim tra dliu v Business LogicMt trong nhng iu quan trng m cc nh pht trin cn ngh n khi lm vic vi d liu l lmsao kt hp c cc php xc thc d liu v cc quy tc chng trnh (business log ic). LINQ to

    SQL cng h tr nhiu cch cc nh pht trin c th d dng tch hp chng vo vi cc m hnhd liu ca h.

    LINQ to SQL cho php bn thm kh nng xc thc d liu m khng ph thuc vo cch bn to ram hnh d liu cng nh ngun dliu. iu ny cho php bn c th lp li cc php kim tra nhiu ch khc nhau, v lm cho m lnh sng sa v d bo tr hn rt nhiu.

    6. H trkim tra cc gi tr thuc tnh da trn schema ca CSDLKhi nh ngha cc lp m hnh d liu dng LINQ to SQL designer trong VS 2008, chng s mcnhin c gn cc quy tc xc thc da trn cu trc nh ngha trong CSDL.

    Kiu d liu ca thuc tnh trong cc lp m hnh d liu s khp vi cc kiu d liu tng ngtrong CSDL. iu ny c ngha l bn sgp li bin dch nu c gng gn mt gi tr kiu booleanv cho mt thuc tnh decimal, hoc nu th p kiu d liu mt cch khng hp l.

    Nu mt ct trong CSDL c nh du cho php mang gi tr NULL, khi thuc tnh tng ngtrong m hnh d liu c to bi LINQ to SQL designer cng cho php NULL. Cc ct khng cho

    php NULL s t ng a ra cc exception nu bn c gng lu mt i tng c thuc tnh mang gi tr NULL. LINQ to SQL s m bo cc ct nh danh/duy nht khng b trng lp trongCSDL.

    Bn c th dng LINQ to SQL designer ghi ln cc quy tc xc thc da trn schema numun, nhng cc quy tc ny s c to ra t ng v bn khng cn lm bt k iu g cho

    php chng. LINQ to SQL cng t ng x l cc chui escape, do vy bn khng cn lo lng v liSQL injection.

    7. H trty bin vic kim tra gi tr cc thuc tnhVic kim tra d liu da trn cu trc nh ngha trong CSDL rt hu ch, nhng ch c coi nh mc c bn, trong thc t c th bn s gp phi nhng yu cu kim tra phc tp hn nhiu.

    Hy xem mt v d trong CSDL Northwind, khi ti nh ngha thuc tnh Phone thuc lp Customerc kiu d liu l nvarchar. Cc nh pht trin dng LINQ to SQL c th vit code ging nh diy cp nht n vimt s phone hp l:

    Vn lon code trnc coi l hp lng t gc kiu d liu SQL, v chui trn vn l

    mt chui nvarcharmc d c th n khng phi l mt s phone hp l:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    42/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    42

    trnh vic thm cc s phone kiu nh trn vo CSDL, chngta c th thm mt quy tc kim tratnh hp l vo lp Customer. Thm mt quy tc kim tra thc s n gin. Tt c nhng gchng ta cn lm l thm mt partial class vo v nh ngha phng thc nh di y:

    on code trn tn dng u im ca 2c tnh trong LINQ to SQL:

    1) Tt c cc lp c to ra u l partial c ngha l nh pht trin c th d dng thm vo ccphng thc, thuc tnh v thm ch c cc s kin (v t chng trong mt file ring bit). iu nylm cho vic thm cc quy tc xc thc v cc hm ph tr vo m hnh d liu v lp DataContextrt d dng. Bn khng cn cu hnh hay vit thm cc code no khc lm c iu ny.

    2) LINQ to SQL to sn mt lot cc im m rng trong m hnh d liu v lp DataContext mbn c th dng thm vo cc php kim tra d liu trc v sau khi thc hin cc cng vic.Nhiu trong s ng dng mt c tnh ngn ng mi c gi l partial method c trong VB vC# c trong VS 2008 beta 2. Wes Dyer trong nhm C# c mt bi ni v cch cc partial methodlm victi y.

    Trong v d v vic kim tra tnh hp l d liu trn, ti dng phng thc OnPhoneChanging, y

    l mt phng thc s c thc thi bt k lc no ngi dng gn li gi tr cho thuc tnh Phonetrn mt i tng Customer. Ti c th dng phng thc ny xc thc gi tr u vo theo btk cch g ti mun (trong v d ny, ti dng mt biu thc chnh quy). Nu gi tr hp l, ti chn gin return v khng lm g c, khi LINQ to SQL s cho l cc gi tr ny l gi tr hp l,ngc li ti c th pht ra mt Exception bn trong phng thc kim tra, v php gn khi skhng c thc hin.

    8. H trty bin vic kim tra tnh hp l ca thc thVic kim tra trn tng thuc tnh nh trong cc v d trn rt hu dng khi bn mun kim tra gitr ca tn thuc tnh ring l. Nhng i khi, bn s cn phi kim tra da trn nhiu gi tr ca cc

    thuc tnh khc nhau.

    http://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspxhttp://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspxhttp://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspxhttp://blogs.msdn.com/wesdyer/archive/2007/05/23/in-case-you-haven-t-heard.aspx
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    43/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    43

    Hy xem v d sau, ti s t gi tr cho 2 thuc tnh OrderDatev RequiredDate:

    (Add c thay i bng InsertOnSubmit trong phin bn hin ti)

    on lnh trn l hp l nu ch n thun xt t gc ngn ng nhng s l khng c nghakhi bnli mun t ngy khch hng yu cutrc ngy t hng.

    Tin vui l t bn LINQ to SQL beta 2, chng ta c th thm vo cc quy tc kim tra cho tngthcth trnh cc li kiu nh trn bng cch thm mt lp partial cho lp Order v hin thc hahm OnValidate(), hm ny s c gi trc khi d liu c a vo CSDL. Bn trong phngthc ny, chng ta c th truy cp v kim tra tt c cc thuc tnh ca lp trong m hnh d liu.

    Bn trong phng thc ny, bn c th kim tra gi tr bt k thuc tnh no, v thm ch c th truy

    cp (ch c) vo cc i tng lin quan, v c th pht ra mt exception nu c tn ti cc gi trkhng hp l. Bt k mt exception no c pht ra t phng thc OnValidate() s lm cho viccp nht b hy b, v hy b cc thay i trong transaction.

    9. Ty bin cc phng thc kim tra vic thm/xa/sa dliuC nhiu lc bn mun thm cc php kim tra khi thm/xa/sa d liu. LINQ to SQL Beta2 cho

    php lm iu ny bng cch cho php bn thm vo mt lp partial m rng lp DataContext vsau hin thc ha cc phng thc ty bin cc thao tc thm/xa/sacho cc thc th. Ccthc ny s cthc thit ng khi bn gi SubmitChanges() trn lp DataContext.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    44/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    44

    Bn c th thm cc php kim tra thch hp vo bn trong cc phng thc v nu d liu hpl, LINQ to SQL s tip tc lu li cc thay i vo CSDL (bng cch gi phng thcExecuteDynamicXYZ ca DataContext).

    Mt trong nhng iu th v l cc phng thc ph hp s c gi t ng, khng ph thuc vong cnh m i tng c to/xa/sa. Hy xemv dsau, y ti mun to mt Order mi vkt hp n vi mt Customer c:

    (Add c thay i bng InsertOnSubmit trong phin bn hin ti)

    Khi ti gi northwind.SubmitChanges() trn, LINQ to SQL s xc nh l n cn lu li mt itng Order, vphng thcInsertOrder s t ng c gi.

    10. Nng cao: Xem danh sch thay i cho Transactioni khi bn mun thm cc quy tc kim tra m khng th ch da trn tng thao tc thm/xa/saring l, thay vo bn phi c th duyt qua ton b cc thao tc thc hin trong transaction.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    45/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    45

    Bt u t bn Beta2 ca .NET 3.5, LINQ to SQL cho php bn truy cp vo danh sch ny bngcch gi phng thc DataContext.GetChangeList(). N s tr v mt i tng ChangeList chacc tp hp cho cc thao tc thm/xa/sa c thc hin.

    Mt cch tip cn l bn c th to mt lp tha k t lp DataContext v override phng thcSubmitChanges(). Khi bn c th ly ChangeList() cho thao tc cp nht v thc hin cc phpkim tra cn thit trc khi thc thi:

    11. Xl cc thay i ng thi vi Optimistic Concurrency:Mt trong nhng vn m cc nh pht trin phi ngh n trong mi trng a ngi dng l lmth no c th x l cc thao tc cp nht trn cc cng mt tp d liu. V d, cho l c hai ngidng ang cng ly v mt i tng product bn trong mt ng dng, v mt ngi t li gi trcho ReorderLevel l 0, trong khi ngi kia t li l 1. Nu c hai ngi dng u lu li cc thayi vo CSDL, nh pht trin cn cn nhc vic x l tranh chp d liu.

    Mt cch tip cn n gin l let the last writer win (ngi cui cng l ngi chin thng) cngha l nhng thay i bi ngi u tin s b thay i m khng bit. V iu ny thng ccoi l mt tri nghim km ci (v khng ng) c ngha ngi dng s cm thy kh s dng.

    Mt cch tip cn khc m LINQ to SQL h tr l dng m hnh optimistic concurrency khi LINQ to SQL s t ng xc nh xem gi tr gc trong CSDL b thay i b ngi dng khc

    hay cha. LINQ to SQL sau s cung cp mt danh sch cc gi tr b xung t ngi pht trinc th chn gii php x l hoc c th yu cu ngi dng chn mt thao tc no h mun.

    Ti s ni v cch dng optimistic concurrency vi LINQ to SQL trong cc bi vit khc.

    12. Dng SPROCs hoc ty bin logic cc cu SQL:Mt trong nhng cu hi m cc nh pht trin (v c bit l cc DBA cc nh qun tr CSDL),nhng ngi tng vit cc th tc (SPROC) vi cc cu SQL ty bin thng hi khi nhn thyLINQ to SQL ln u tin l: lm sao ti c th kim sot hon ton cc cu lnh SQL c thcthi bn di ?

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    46/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    46

    Mt tin tt l LINQ to SQL c mt m hnh cc k mm do, n cho php cc nh pht trin c ththay th cc cu lnh caLINQ to SQL bng cc th tcinsert, update, delete m h t nh ngha.

    iu thc s th v l bn c th bt u bng cch nh ngha m hnh d liu ca ring bn v LINQ to SQL t thc hin cc thao tc thm/sa/xa. Ri sau bn c th ty bin li m hnh dliu thc hin cc thao tc cp nht vi cc th tc hoc cc cu SQL ca bn m khng phi thayi bt k on lnh no dng m hnh d liu , v cng chngphi thay i bt k quy tc kimtra to trc . iu ny cung cp kh nng ty bin rt ln cho bn khi xy dng ng dng.

    Ti cng s ni k hn v cch ty bin m hnh d liu dng cc th tc hay cu lnh SQL trongmt bi vit khc.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    47/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    47

    Bi 5: S dng asp:LinqDataSource

    Trong bi vit ny, ti s khm ph control mi c trong ASP.NET thucphin bn .NET 3.5. Control ny l mt datasource control mi cho ASP.NET (ging

    ObjectDataSource v SQLDataSource c trong ASP.NET 2.0) cho php bn khai bo vic gn ktd liu vo m hnh d liu ca LINQ to SQL cc k d dng.

    1. ng dng mu m chng ta s xy dng:Chng trnh web chnh sa d liu n gin m ti s xy dng qua cc bc c m t trong biny s l mt chng trnhcho php nhp/chnh sa d liu cho cc sn phm trong mt CSDL:

    Chng trnh s h tr ngi dng cc tnh nng sau:

    1. Cho php ngi dng lc sn phm theo phn loi.2. Cho php ngi dng sp xp cc sn phm bng cch nhp chut ln tiu ct (Name, Price, Units

    In Stock, ).3. Cho php ngi dng phn trang cc sn phm (10 sn phm mi trang).4. Cho php ngi dng chnh sa v cp nht cc chi tit sn phm ngay trn trang.5. Cho php ngi dng xa cc sn phm trong danh sch.

    ng dng web ny s c xy dng vi mt m hnh d liu hng i tng dng LINQ to SQL.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    48/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    48

    Tt c cc quy tc x l v kim tra d liu s c xy dng trong lp d liu m khng phitrong lp giao din. iu ny s m bo rng: 1) mt tp cc quy tc x l ng nht s c dng tt c mi ch trong ng dng, 2) chng ta s phi vit t code m khng cn lp li, v 3) c th ddng chnh sa/thay i cc quy tc x l sau ny m khng cn cp nht li chng nhiu ch khcnhau trong ng dng.

    Chng ta cng s tn dng c u im ca vic phn trang/sp xp bn trong LINQ to SQL m bo rng cc c tnh khng c thc hin bn trong lp gia (middle -tier), m s cthc hin trong CSDL (c ngha l ch c 10 sn phm c ly ra trong CSDL ti mt thi im,chng ta s khng ly hng ngn dng ri mi thc hin phn trang hay sp xp trn web server).

    2. l g v n gip g cho chng ta?Control l mt ASP.NET control hin thc ha m hnhDataSourceControlc gii thiu trong ASP.NET 2.0. N tng t nh cc control ObjectDataSource v

    SqlDataSource, bn c th dng n khai bo vic gn ni d liu gia mt control ASP.NET vimt ngund liu. im khc bit l thay v n gn ni trc tip vo CSDL (nh SqlDataSource)hay vo mt lp (ObjectDataSource), c thit k gn vo mt m hnh dliu LINQ.

    Mt trong nhng u im ca vic dng l n tn dng c tnh mm do cacc trnh cung cp LINQ (LINQ provider: nh LINQ to SQL, LINQ to Object). Bn khng cnnh ngha cc phng thc query/insert/update/delete cho ngun d liu gi, thay vo bn cth tr n m hnh d liu ca bn, ch ra bn thc th no bn mun lmvic, ri gn ni n vo mt control Asp.NET v cho php chng lm vic vi nhau.

    V d, xy dngmt danh sch c bn cc sn phm cho php lm vic vi cc thc th Producttrong m hnh d liu LINQ to SQL, ti c th khai bo mt th trn trang vtr vo lp datacontext ca LINQ to SQL, v ch ra cc thc th (v d: Products) trong m hnhLINQ to SQL m ti mun gn ni. Ti c th cho mt GridView tr vo n (bng cch t thuctnh DataSourceID) cho php xem cc Product theo dng li:

    Khng cn lm thm bt k iu g, ti c th thc thi trang web v c mt danh sch cc Productvi kh nng phn trang cng nh sp xp c tch hp sn. Ti cng c th thm mt ntedit/delete v cho php ngi dng chnh sa d liu. Ti khng cn thm bt k phng thc, nhx cc tham s, hay thm ch vit bt k cu lnh no cho x l cctrng hp hin th v cp nht trn n c th lm vic vi m hnh LINQ to SQL m chng ta chn v thc hin cc thao tc t ng. Khi cp nht, LINQ to SQL s m bo rng cc quy tc x l

    v kim tra d liu m ta thm vo m hnh LINQ to SQL (di dngcc phng thc partial)cng s c thc hin trc khi d liu c thc s cp nht vo CSDL.

    http://msdn2.microsoft.com/en-us/library/ms227679.aspxhttp://msdn2.microsoft.com/en-us/library/ms227679.aspxhttp://msdn2.microsoft.com/en-us/library/ms227679.aspxhttp://msdn2.microsoft.com/en-us/library/ms227679.aspx
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    49/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    50/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    50

    Bc 2: To danh sch sn phm

    Chng ta s bt u phn giao din bng cch to mt trang ASP.NET vi mt control v dng CSS nh dng:

    Chng ta c th vit code gn ni m hnh d liu vo gridview ny (ging nh ti lm trongphn 3), hoc ti c th lm cch khc l dng control mi gn ni gidviewny vi m hnh d liu.

    VS 2008 includes build-in designer support to make it easy to connect up our GridView (or any otherASP.NET server control) to LINQ data. To bind our grid above to the data model we createdearlier, we can switch into design-view, select the GridView, and then select the New Data

    Source option within the Choose Data Source: drop-down:

    Trnh thit ktrong VS 2008 c sn kh nng h tr lm iu ny mt cch d dng vi GridView(hay bt k control ASP.NET no khc) vod liu LINQ. gn ni, chng ta c th chuyn sangch thit k, chn GridView, v sau chn New Data Source bn trong dang sch ChooseData Source::

    Mt hp thoi s hin ln, trong c danh sch cc loi datasource, chn LINQ trong hp thoi nyv t trn cho control m bn mun to:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    51/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    51

    Trnh thit k s hin th tip cc lp DataContext ca LINQ to SQL m ngdng ca bn c th dngc bao gm c trong cc th vin m bn ang tham chiu ti):

    Chng ta mun chn m hnh d liu c to trc y vi trnh thit k LINQ to SQL. Chngta cng s mun chn bng d liu bn trong m hnh d liu m chng ta s coi nh thc th chnh

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    52/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    52

    lm vic vi . Trong v d ny chng ta s chn thc th Products. Chngta cng s nhn vo nt Advanced v cho php vic cp nht cng nh xa d liu:

    Khi nhn vo nt Finish trn, VS 2008 s khai bo mt trong trang .aspx,v cp nht tr n n (thng qua thuc tnh DataSourceID).N cng s tngto ra cc ct trong Grid d trn cu trc ca thc th Products m chng ta chn:

    Chng ta cng c th nhp vo hnh tam gic nh bt ln smart task ca GridView v ch rachng ta mun cho php vic phn trang, sp xp, chnh sa v xa d liu:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    53/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    53

    Chng ta c th nhn F5 thc thi, v c mt trang hin th danh sch sn phm vi y khnng phn trang cng nh sp xp cc ct:

    Chng ta cng c th nhn v cc nt edit hoc delete cp nht li d liu:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    54/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    54

    Nu nhn vo m ngun ca trang, chng ta s thy cc th ca trang cha ni dung ging nh diy. Th ch n lp DataContext ca LINQ to SQL m ta to trc y,cng nh bng d liu m chng ta mun dng. GridView sau ch n (thng qua DataSourceID) v ch ra nhng ct no s c hin th, tiu ct, cng nh cch spxp s c dng khi tiu ct c chn.

    Gi chng ta c mt trang web c bn lm vicvi m hnh d liu LINQ to SQL, chng ta cth tip tc ty bin giao din v hnh vi.

    Bc 3: B cc ct khng cn thit

    GridView ca chng ta trn c rt nhiu ct c nh ngha sn, 2 trong s (SupplierID vCategoryID) l cc ct kha ngoi, v vic hin th cc ct ny c v nh khng phi l mt tnghay.

    Xa bt cc ct khng cn thit

    Chng ta c th bt u vic dn dp giao din bng cch xa i mt s ct khng cn thit. Ti cth lm iu ny bng cch sa m ngun, hay trong ch thit k (nhp chut ln ct mun xa vchn Remove). V d, bn c th b ct Quantity Per Unit di y v chy li ng dng cachng ta c mt giao din sng sa hn:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    55/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    55

    Nu bn tng dng control trc y v truyn cc tham s cho ccphng thc cp nht, mt trong nhng th khn kh bn bit c l l vic thay i tham s ca ccphng thc cp nht trong TableAdapter khi cc thng s c nhn t lp giao din b thay i.V d: nu chng ta xa mt ct trong bng trn, chng ta li phi chnh sa li TableAdapter n h tr cc phng thc cp nht khng cn ti tham s b xa .

    Mt iu hay l vi bn khng cn thc hin cc thay i kiu nh vy. Ch

    n gin l thm hoc xa mt ct v chy li chng trnh khng cn lm thm bt c iu gkhc. iu ny lm cho vic thay i giao din web dng d hn nhiu.

    Xa cc ct SupplierID v CategoryID

    Hin ti, chng ta hin th cc gi tr kha ngoi n cc bng Supplier v Category trong GridView:

    iu ny tuy cn thit ng t gc m hnh d liu, nhng li khng mang li gi tr g cho ngidng. Th m chng ta lm l hin th CategoryName v SupplierName, v cung cp mt dang schx xung trong ch Edit cho php ngi dng d dng chn cc gi tr cho SupplierID vCategoryID.

    Ti c th thay i GridView hin th Supplier Name v Category Name thay v ID bng victhay th vi mt . Trong TemplateField ny, tic th thm

    bt k ni dung no ti mun ty bin li cch hin th ca ct d liu.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    56/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    56

    Trong on m di y, ti s tn dng cc thuc tnh Supplier v Category trn mi Product, nh ti c th d dng gn ni cc ct Supplier.CompanyName v Category.CategoryName v cc cttng ng trong Grid.

    V by gi khi chy ng dng, ti s c danh sch cc Category v Supplier theo tn:

    to ra danh sch cho php ngi dng chn cc gi tr ca cc ct Supplier v Category trong ch Edit, u tin ti s thm hai control na vo trang. Ti s cu hnh chng gn ni vi Categories v Suppliers bn trong m hnh d liu LINQ to SQL m ta to trcy:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    57/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    57

    Ti c th quay tr li cc ct m chng ta to v ty bin giao din Editca chng (bng cch ch ra EditItemTemplate). Chng ta cng s ty bin mi ct c mt danhsch trong ch Edit, v cc gi tr s c ly t cc datasource CategoryDataSource vSupplierDataSource trn, v cc mt lin h ny s l 2 chiu:

    V gi, khi ngi dng nhp chut ln Edit trn GridView, chng s c hin th nh mt danhsch Supplier m sn phm ang chn kt hp:

    V khi bn bm nt Save, sn phm s c cp nht mt cch ph hp (GridView s dng gi trca dng c chn hin ti trong DropDownList a vo SupplierID).

    Bc 4: Lc danh sch sn phm

    Thay v hin th tt c cc sn phm trong CSDL, bn c th cp nht phn giao din n thm mtdanh sch cho php ngi dng lc li cc sn phm theo mt phn loi no .

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    58/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    58

    V chng ta thm control tham chiu n Categories vo trang web nytrc y, do vy gi nhng g cn lm ch l to mt dropdownlist trn u trang gn ni vi n.V d:

    Khi ti chy trang web ny,ti s c trn u trang mt danh sch cho tt c cc mc phn loi:

    Bc cui cng l cu hnh GridView n ch hin th cc sn phm trong phn loi c chn,cch d nht l chn Configure DataSource trong smart task ca GridView:

    N s a ti quay tr li ca s thit k m ti dng trong phn u bivit ny. Ti c th chn nt Where trong ca s ny thm mt b lc vo control datasource.Ti c th to ra nhiu b lc nu cn, v ko cc gi tr lc t mt vi ch khc nhau (v d: tquerystring (trn web), t cc gi tr trn form, t cc control khc trn trang):

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    59/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    59

    trn, ti s to b lc cc Products theo CategoryID, v sau ly gi tr ca CategoryID mun lct danh sch m chng ta to trn trang:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    60/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    60

    Sau khi bm Finish, control trn trang ca chng ta s c cp nht sdng b lc ging nh sau:

    V by gi nu thc thi trang web, ngi dng s c th chn mt trong cc phn loi c sn v sau phn trang, sp xp, chnh sa hay xa cc sn phm trong phn loi :

    Control s t ng p dng cc b lc LINQ cn thit khi lm vic vi cclp LINQ to SQL ca chng ta m bo rng ch c cc d liu cn thit c ly v t CSDL (vd: trong Grid trn, ch c 3 dng sn phm t trang th hai trong nhm cc sn phm Confection

    c ly v).

    Bn c th s dng s kin Selecting trn nu mun ty bin cu truy vnLINQ trong on code.

    Bc 5: Thm cc quy tc kim tra logic

    Nh ti ni n trong phn 4 ca lot bi LINQ to SQL ny, khi chng ta nh ngha m hnh dliu LINQ to SQL, mc nhin chng ta s t ng c mt tp hp cc rng buc trong cc lp mhnh d liu, cc rng buc ny c sinh ra da trn nh ngha trong CSDL. iu ny c ngha lnu bn th nhp mt gi tr null vo cho mt ct mandatory, gn mt string vo cho mt ct s

    nguyn, hay t gi tr cho kha ngoi cho mt dng khng tn ti, m hnh LINQ to SQL ca chngta s pht ra mt li v nh vy CSDL c ton vn.

    Vic kim tra theo cch ny ch nhm m bo s ton vn mc c bn, d vy, n vn cho huht cc ng dng trong thc t. Chng ta cng c th mong mun thm vo cc quy tc logic mtmc cao hn, cho php kim tra cc quy tc business vo trong cc lp m hnh d liu. Xin cmn LINQ to SQL cho php lm iu ny tht d dng ( xem chi tit, xin c li phn 4).

    Mt v d v cc quy tc logic

    Ly v d, ngoi nhng quy tc logic c bn, chng ta cn mun m bo rng ngi dng s khngth ngng bn mt loi sn phm nu vn cn sn phm loi trong kho hng.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    61/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    61

    Nu mt ngi dng nhn nt Save dng trn, chng ta s khng cho php vic thay i c luli v pht ra mt li bo cho ngi dng.

    Thm mt quy tc kim tra m hnh d liu

    Nu kim tra cc quy tc ny lp giao din th s l khng ph hp, v khi quy tc ny s chc p dng cho chnh ni , v s khng t ng c p dng nu chng ta thm mt trang khccng cho php cp nht Product vo ng dng. Vic phn tn cc quy tc kim tra logic/business volp giao din s lm cho vic bo tr tr nn kh khn khi ng dng tr nn ln v phc tp, v ccthay i/cp nht u cn p dng cc thao tc cn thit nhiu ch khc nhau.

    Ni ccoi l ph hp t cc quy tc kim tra ny l trong cc lp m hnh d liu LINQ toSQL m chng ta nh ngha trc y. Nh cp n trong phn 4, tt c cc lp c sinhra bi LINQ to SQL designer u c nh ngha nh cc lp partial n cho php chng ta cth d dng thm vo cc phng thc/s kin/thuc tnh. Cc lp m hnh d liu LINQ to SQL s

    t ng gi cc phng thc kim tra m chng ta c th vit ra thc hin vic kim tra theomong mun ring.

    V d, ti c th thm mt lp partial vo ng dng hin thc phng thc OnValidate() mLINQ to SQL s gi trc khi lu mt i tng Product vo CSDL. Bn trong phng thc ny tic th thm quy tc sau m bo rng cc sn phm khng th c mt ReOrder Level nu sn

    phm ngng bn:

    Mt khi thm lp trn vo d n, quy tc business trn s c p dng bt k lc no ngidng dng n m hnh d liu v chnh sa li CSDL. iu ny c p dng cho c vic thm mtsn phm mi, cng nh cp nht li mt sn phm c.

    V m chng ta nh ngha trn lm vic vi cc lp m hnh d liu

    LINQ to SQL, do vy cc thao tc cp nht/xa/thm u phi qua c php kim tra trn trckhi c p dng vo CSDL. Chng ta khng cn lm thm bt k iu gi lp UI php kim tra

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    62/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    62

    ny c thc hin n s t ng c dng bt k ni no cng nh bt k lc no m hnhLINQ to SQL c dng.

    Thm phn kim sot li vo giao din

    Mc nhin nu ngi dng nhp vo mt gi tr khng hp l cho UnitsOnOrder/Discontinued voGridView, cc lp LINQ to SQL ca chng ta s sinh ra mt exception. n lt s bt li ny v cung cp mt s kin m ngi s dng c th dng xl li . Nu khng c trnh x l li no c cung cp, khi GridView (hoc mt control khc)gn ni vo s bt li ny v cung cp mt event ngi dng c th x ln. Nu li tip tc khng c ai x l li, khi n s c chuyn n cho Page, v chuyn nhm x l Application_Error() trong file Global.asax nu vn khng c trnh x l li. Cc nh phttrin c th chn bt k ch no trong chui x l ny cung cp mt cch tng tc hp l nhtn ngi dng cui.

    i vi ng dng ca chng ta, ni hp l nht n x l cc li cp nht d liu l bt s kinRowUpdatedtrn GridView. S kin ny s c pht ra mi khi mt lnh cp nht c thc hintrn datasource, v chng ta c th truy cp thng tin chi tit ca exception nu vic cp nht khngthnh cng, sau o hin th thng bo thch hp cho ngi dng.

    rng trn ti khng h thm bt k hm kim tra no vo lp giao din. Thay v vy, ti s lyv chui thng bo li ca exception pht ra t phn business logic v hin th n cho ngi dng.

    Ch l ti cng ch ra trn l ti mun GridView vn trong ch Edit khi li xy ra bngcch ngi dng s khng b mt i nhng thay i m h to ra, v c th chnh sa cc gitr h nhp vo v click nt update mt ln na lu li. Chng ta cng c th thm mtcontrol vi ID ErrorMessage bt k ch no m ta mun thng bo li hin ra:

    V by gi chng ta s th cp nht Product vi cc gi tr kt hp khng hp l, chng ta s thy

    mt thng bo li, nh ngi dng s bit cch sa li cho ph hp:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    63/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    63

    Mt trong nhng u im khi lm theo cch trn l ti c th thm hay thay i cc quy tc trong mhnh d liu m khng cn chnh sa li code trong lp giao din c th hin th thng bo phhp. Cc quy tc xc thc, v thng bo li tng ng, c th c vit mt ch trong lp m hnhd liu v s c p dng ph hp bt k khi no bn dng n.

    3. Tng ktControl cung cp mt cch d dng gn ni bt k control ASP.NET vomt m hnh d liu LINQ to SQL. N cho php cc control dng hin th giao din c th va ly

    d liu v t LINQ to SQL, cng nh p dng cc thay i thm/xa/sa vo m hnh d liu.

    Trong ng dng trn, chng ta dng LINQ to SQL designer to ra mt m hnh d liu rrng v hng i tng. Chng ta sau thm ba control ASP.NET vo trang (GridView,DropDownList, ErrorMessage Literal), v thm ba control gn ni d liucho Product, Category, v Supplier.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    64/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    64

    Chng ta sau vit thm 5 dng kim tra d liu trong lp m hnh d liu, v 11 dng tronglp giao din x l li.

    Kt qu cui cng l mt ng dng web n gin vi giao din c ty bin cho php ngi dnglc d liu ng theo phn loi, sp xp v phn trang mt cch hiu qu trn danh sch sn phm,chnh sa trc tip thng tin sn phm v cho php lu li cc thay i, v xa cc sn phm t hthng.

    Trong bi vit tip theo ca lot bi ny, chng ta s khm ph thm LINQ to SQL bao gm kimsot truy xut ng thi, lazy loading, tha k cc nh x bng, cng nh cch dng cc th tc SQL ty bin.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    65/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    65

    Bi 6: Ly d liu dng Stored Procedure

    Trong bi vit hm nay, ti s cho thy cch chng ta c th dng cc stored procedure (SPROCs) vcc hm do ngi dng nh ngha (UDFs) vi m hnh d liu LINQ to SQL. Bi vit ny s tp

    trung ch yu vo cch dng SPROCs truy vn v ly d liu v t CSDL. Trong bi vit k tip,ti s hin th cch bn c th dng cc SPROCs cp nht, thm, xa d liu t CSDL.

    1. Dng SPROC hay khng SPROC? l mt vn .Cu hi liu nn dng cc cu SQL ng c sinh ra bi trnh ORM hay dng Stored Procedure khixy dng lp d liu l mt ch khng bao gi kt thc tranh ci gia cc nh pht trin, kin trcs phn mm v cc DBA. Rt nhiu ngi thng minh hn ti nhiu vit v ch ny, v vyti s khng ni thm v vn ny y na.

    LINQ to SQL i cng vi .NET 3.5 rt mm do, v c th c dng to cc lp m hnh d

    liu,trong cc i tng khng ph thuc vo cu trc CSDL pha di, v c th x l cc phpkim tra logic cng nh xc thc tnh hp l ca d liu m khng ph thuc vo vic d liu sc lu np dng cc cu SQL ng hay thng qua cc SPROCs.

    Trong biTruy vn C s d liu (phn 3), ti tho lun cch bn c th vit cc biu thc truyvn LINQ cho mt m hnh d liu LINQ to SQL dng on m nh sau:

    Khi bn vit cc biu thc LINQ kiu nh vy, LINQ to SQL s thc thi cc cu lnh SQL ng bn c th ly v cc i tng khp vi cu truy vn ca bn.

    Nh bn c hc trong bi vit ny, bn cng c th dng cc SPROCs trong CSDL trong lpDataContext, n cung cp mt cch khc ly v cc i tng Products bng cch gi th tc

    tng ng:

    http://namdh.wordpress.com/2009/05/17/2008/11/21/linq-to-sql-part-3-querying-database/http://namdh.wordpress.com/2009/05/17/2008/11/21/linq-to-sql-part-3-querying-database/http://namdh.wordpress.com/2009/05/17/2008/11/21/linq-to-sql-part-3-querying-database/http://namdh.wordpress.com/2009/05/17/2008/11/21/linq-to-sql-part-3-querying-database/http://namdh.wordpress.com/2009/05/17/2008/11/21/linq-to-sql-part-3-querying-database/
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    66/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    66

    Kh nng ny cho php bn dng c cc cu SQL ng v cc SPROCs vi mt m hnh d liu r

    rng, mnh m cng nh cung cp s mm do khi lm vic vi cc d n.

    2. Cc bc nh x v gi SPROC dng LINQ to SQLTrong phn 2, ti ni v cch dng LINQ to SQL designer to ra mt m hnh d liu LINQ toSQL nh di y:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    67/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    67

    ca s trn c cha 2 ca s con, ca s bn tri cho php chng ta nh ngha m hnh d liu snh x vo CSDL, ca s bn phi cho php nh x cc th tc v hm vo i tng DataContext,iu ny cho php chng ta c th thay th cc cu SQL ng trong vic ly d liu v.

    3. Cch nh x mt SPROC vo mt DataContext ca LINQ nh x mt SPROC vo lp DataContext, trc tin hy m ca s Server Explorer trong VS2008 v m danh sch cc SPROC trong CSDL:

    Bn c th nhy p vo bt k th tc SPROC no trn m v chnh sa chng, v d nhCustOrderHist trong Northwind nh di y:

    nh x vo SPROC trn vo DataContext, bn c th ko/th n t ca s Server Explorer lntrn ca s LINQ to SQL designer. Vic ny s lm t ng sinh ra mt th tc trong lpDataContext ca LINQ to SQL nh di y:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    68/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    68

    Mc nhin tn ca phng thc c to trong lp DataContext s chnh l tn ca SPROC, v kiutr v ca phng thc s l mt kiu c to t ng vi cch t tn theo dng[SprocName]Result. V d: SPROC trn s tr vef mt dy cc i tng c kiuCustOrderHistResult. Chng ta c th i tn ca phng thc nu mun bng cch chn n ridng Property Grid t li tn khc.

    4. Cch gi SPROC mi c toKhi hon thnh cc bc trn nh x mt SPROC vo lp DataContext ca chng ta, bn c th gi n mt cch d dng ly d liu v. Tt c nhng g chng ta cn lm l gi phng thcm chng ta nh x trong DataContext ly v mt chui cc i tng v t SPROC:

    VB:

    C#:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    69/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    69

    Thm na, thay v lp qua tp kt qu nh trn, ti cng c th gn ni n vo cho mt control hin th ra mn hnh, v d nh ti c th dng :

    Khi danh sch cc sn phm c mua b khch hng s c hin th nh sau:

    5. nh x kiu tr v ca phng thc SPROC vo mt lp trong m hnh dliuTrong th tc CustOrderHist trn, th tc tr v mt danh sch d liu bao gm 2 ct:ProductName cha tn v TotalNumber cha s sn phm c t hng trong qu kh. LINQ to

    SQL designer s t ng to ra mt lp c tn CustOrderHistResult biu din kt qu ny.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    70/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    70

    Chng ta cng c th chn cch gn kiu tr v ca th tc cho mt lp c sn trong m hnh dliu, v d mt lp thc th Product hay Order.

    V d, cho l chng ta c mt th tc tnGetProductsByCategory trong CSDL tr v thng tin snphm ging nh sau:

    Cng nh trc y, ta c th to mt phng thc GetProductsByCategory bn trong lp

    DataContext m n s gi th tc ny bng cch ko n vo ca s LINQ to SQL designer. Thay vth n vo mt v tr bt k, chng ta s th n ln trn lp Product m ta to ra sn trn sa sny:

    Vic ko mt SPROC v th ln trn mt lp Product s lm cho LINQ to SQL Designer to raphng thc GetProductsByCategory tr v mt danh sch cc i tng c kiu Product:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    71/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    71

    Mt u im ca vic s dng lp Product nh kiu tr v l LINQ to SQL s t ng qun l ccthay i c to ra trn i tng c tr v ny, ging nh c lm vi cc i tng c trv thng qua cc cu truy vn LINQ. Khi gi SubmitChanges() trn DataContext, nhng thay iny cng s c cp nht tr li CSDL.

    V d, bn c th vit on code ging nh di y (dng mt SPROC) v thay i gi ca cc snphm bn trong mt Category no thnh 90% gi tr c:

    Khi gi SubmitChanges, n s cp nht li gi ca tt c cc sn phm. hiu thm v cch qun l

    cc thay i v cch phng thc SubmitChanges() lm vic, cng nh cc thm cc phng thcxc thc logic d liu, xin mi c li bi 4 trong cng lot bi ny.

    Trong bi vit tip theo ca lot bi v LINQ to SQL, ti s hng dn cc bn cch thay th cc culnh SQL ng cho vic INSERT/UPDATE/DELETE bng cc th tc SPROC. V khi thay th nhvy, bn hon ton khng phi thay i g trn cc on lnh trn vic thay i ny hon ton xyra trn m hnh d liu v hon ton trong sut vi cc chng trnh dng n.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    72/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    72

    6. Xl cc tham s th tc dng OUTPUTLINQ to SQL nh x cc tham s dng OUTPUT ca cc SPROC thnh cc tham bin (dng tkha ref trong C# hoc ByRef trong VB.NET), v vicc tham tr, LINQ to SQL dng cc bin kiu

    nullable (dng ? trong C# hay trong VB.NET).V d, th tcGetCustomerDetails sau s nhn vo mtCustomerID nh tham s u vo, v tr vtn cng ty nh mt tham s dng OUTPUT v lch s giao dch nh kt qu truy vn:

    Nu bn ko th tc trn th vo lp Order trong LINQ to SQL designer, chng ta c th vit lnhnh sau gi n:

    VB:

    C#:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    73/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    74/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    74

    C#:

    Mt khi thm phng thc ny vo d n, bn c th gi v chuyn v kiu thch hp l Prod ucthoc Order:

    VB:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    75/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    75

    C#:

    H trcc hm do ngi dng tnh ngha (UDF)

    Thm vo vic h tr cc cc th tc, LINQ to SQL cn h tr cc hm tr v cc gi tr v hnghoc cc bng kt qu. Mt khi c thm vo lp DataContext nh mt phng thc, bn c thdng cc hm UDF ny trong cu trong cc cu lnh LINQ.

    V d, hy xem cc hm UDF n gin c tn MyUpperFunction sau y:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    76/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    76

    Chng ta c th ko v th n t ca s Server Explorer ln ca s LINQ to SQL Designer thmn vo lp DataContext nh mt phng thc.

    Chng ta sau c th dng hm UDF ny ngay bn trong cc biu thc LINQ khi vit cc cu truyvn (ging nh chng ta ang dng trong biu thc Where nh di y):

    VB:

    C#:

    Nu bn dngLINQ to SQL Debug Visualizerm ti vit ti y, bn c th thy cc LINQ toSQL chuyn i cu truy vn trn thnh cu lnh SQL thc thi hm UDF khi chy:

    http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspxhttp://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspxhttp://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspxhttp://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspxhttp://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspxhttp://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspxhttp://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspxhttp://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    77/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    77

    8. Tng ktLINQ to SQL supports the ability to call Stored Procedures and UDFs within the database and nicelyintegrate them into our data model. In this blog post I demonstrated how you can use SPROCs to

    easily retrieve data and populate our data model classes. In my next blog post in this series Ill coverhow you can also use SPROCs to override the update/insert/delete logic when you SubmitChanges()on your DataContext to persist back to the database.

    LINQ to SQL h tr kh nng gi cc th tc v hm trong CSDL v ckh nngtch hp d dngvo trong m hnh d liu. Trong bi vit ny ti trnh din cch dng cc th tc SPROC ddng truy xut cng nh cp nht cc lp m hnh d liu. Trong bi k tip ti s biu din cchdng SPROC thc hin vic cp nht/thm/xakhi gi SubmitChanges cp nht li d liu voCSDL.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    78/103

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    79/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    79

    V d, bn c th thm mt quy tc m bo rng s in thoi ca khch hng c nhp ngnh dng, v chng ta khng cho php thm mt n hng (Order) nu trng OrderDate ln hnRequiredDate. Mt khi c nh ngha nh di y, cc phng thc kim tra s t ng cthc thi bt k lc no chng ta cp nht li cc i tng trong h thng.

    VB:

    C#:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    80/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    80

    Thm phng thc GetCustomer() vo lp DataContext

    Hin ti chng ta to cc lp m hnh d liu, v p dng cc phng thc kim tra trn chng,chng ta c th truy vn v tng tc vi d liu. Chng ta c th lm c iu ny bng cch vitcc cu lnh LINQ vi cc lp m hnh d liu truy vn v cp nht CSDL (ti c ni v iuny trong bi 3). Thm na ti cng c th nh x cc SPROC vo lp DataContext v dng chng a d liu vo CSDL (bi 6).

    Khi xy dng cc lp d liu LINQ to SQL, bn s thng c nhu cu a cc cu lnh LINQthng dng vo cc phng thc tin ch trong lp DataContext. Bn c th lm c iu ny

    bng cch thm mt lp partial vo project. V d, banks c th thm mt phng thc c tn

    "GetCustomer()" cho php chng ta tm kim v ly v cc i tng Customer t CSDL da trnda tr ca CustomerID:

    VB:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    81/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    81

    C#:

    10. Bc 2: Dng lp truy cp dliu (cha sdng SPROC)Hin ti chng ta c mt lp truy cp d liu (data access layer) biu din m hinh d liu, tchhp cc quy tc v cho php chng ta c th thc hin truy vn, cp nht, thm v xa d liu.

    Hy xem mt trng hp n gin l khi chng ta ly v mt i tng khch hng c, cp nht

    li gi tr ca trng ContactName v PhoneNumber, sau to mi mt i tng Order v kt hpchng vi nhau. CHng ta c th vit on lnh di y lm tt c iu ny trong mttransaction. LINQ to SQL s m bo cc th tc kim tra s c thc thi v cho php trc khi dliu c th c cp nht mt cch thc s:

    VB:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    82/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    82

    C#:

    LINQ to SQL theo di cc thay i m chng ta to trn cc i tng c ly v tDataContext, v cng theo di c cc i tng m chng ta thm vo. Khi gi SubmitChanges(),LINQ to SQL s kim tra xem d liu c hp l hay khng, v c ng vi cc quy tc logic haykhng, nu ng th cc cu SQL ng s c sinh ra cp nht bn ghi Customer trn, v thmmt bn ghi mi vo bng Orders.

    11. Chmt giy - Ti ngh bi vit ny nh ni v vic dng SPROC cm ???Nu vn ang c bi ny, bn c l s cm thy kh hiu v khng thy ni g v SPROC. Ti saoti hng dn bn cch vit lnh lm vic vi cc i tng trong m hnh d liu, ri cho phpcc cu lnh SQL ng c thc thi? Sao ti vn cha cho cc bn thy cch gi cc SPROC thc hin vic chm/sa/xa d liu ?

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    83/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    83

    L do l v m hnh lp trnh ca LINQ to SQL lm vic vi cc i tng m hnh d liu bngSPROC cng hon ton tng t vi vics dng cc cu SQL ng. Cch chng ta thm cc quytc kim tra cng hon ton tng t (do vy cc quy tc m ta thm vo trc y s vn c hiuqu khi chng ta chuyn sang dng SPROC). on lnh trn ly v mt Customer, ri cp nhtv thm mt Order s hon ton ging nhau, khng ph thuc vo vic chng ta dng cc cu SQL

    ng hay cc SPROC thc hin vic truy cp vo CSDL.

    M hnh lp trnh ny rt mnh m theo c hai ngha: n khng bt bn phi hc hai cch dng khcnhau, v bn cng khng cn phi quyt nh ngay t u l dung SPROC hay khng. Ban u, bnc th dng cc cu SQL ng c cung cp bi LINQ to SQL cho tt c cc cu truy vn, chn,cp nht v xa d liu. Bn sau c th thm vo cc quy tc kim tra tnh hp l ca d liu,v ri sau na li c th thay i dng cc SPROC - hoc khng ty bn quyt nh. Cc onlnh v cc on test bn vit trc y s vn c s dng tip, khng ph thuc vo vic dngSQL hay SPROC.

    Phn tip theo ca bi nys biu din cch cp nht m hnh d liu m chng ta to ra dngSPROC trong vic thm/sa/xa d liu, chng ta vn tip tc dng cc quy tc xc thc, v vn tiptc lm vic vi cng cc on lnh vit trn.

    12. Cch sdng SPROC thc hin Insert/Update/DeleteChng ta c th sa li lp truy cp d liu c xy dng trc y x l cc thao tc cpnht, thay v dng cc cu SQL ng, theo mt trong 2 cch sau:

    1) Dng LINQ to SQL designer cu hnh cc SPROC thc thi khi gp thao tc thm/xa/sad liu trn cc lp m hnh d liu.

    hoc:

    2) Thm mt lp partial NorthwindDataContext vo d n, ri vit cc phng thc partial tngng vi cc thao tc Insert/Update/Delete (v d: InsertOrder, UpdateOrder, DeleteOrder) m nsc gi khi chng ta thc hin Insert/Update/Delete trn cc i tng m hnh d liu. Cc

    phng thc partial s c truyn vo cc i tng d liu m ta mun cp nht, v chng tac th thc thi cc th tc hay cu lnh SQL m chng ta mun dng lu i tng vo CSDL.

    Khi dng cch 1) (dng LINQ to SQL designer) cu hnh cc SPROC gi, th thc ra n cngs to ra cch lnh tng t nh chng ta dng trong cch 2). Ni chng ti khuyn cc bn dngLINQ to SQL designer cu hnh cc SPROC trong 90% trng hp - v ch trong cc trng hpno bn cn ty bin li cch gi mt mc cao, bn mi nn vit cc lnh mt cch trc tip.

    13. Bc 3: Thm mt Order bng cch dng SPROCChng ta s bt u chuyn m hnh d liu sang dng SPROC, bt u t i tng Object.

    u tin, chng ta n ca s "Server Explorer" m rng nhnh Stored Procedures trong CSDL cachng ta, v sau nhn phi chut v chn "Add New Stored Procedure":

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    84/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    84

    Sau ta to thm mt th tc c tn "InsertOrder" c nhim v chn thm mt bn ghi mi vobng Orders:

    Hy ch cch SPROC nh ngha tham s OrderID nh mt tham s dng OUTPUT. l v ctOrderID trong CSDL l ct t tng mi khi thm mt bn ghi mi vo. Ngi gi s truyn gi trNULL khi gi n - v th tucjnafy s tr v gi tr ca OrderID mi c to ra (bng cch gi hmSCOPE_IDENTITY() cui th tc).

    Sau khi to ra SPROC, chng ta s m LINQ to SQL designer ca lp truy cp dliu. Nh ti

    ni trong bi 6, chng ta c th ko/th cc SPROC t Server Explorer ln trn mn hnh chnh catrnh thit k. Chng ta cng s lm iu tng t vi th tc InsertOrder va c to:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    85/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    85

    Bc cui cng l cu hnh li lp truy cp d liu dng th tc SPROC khi chn cc i tngOrder mi vo trong CSDL. Chng ta c th l iu ny bng cch chn lp Order trong ca sooeLINQ to SQL designer, v sau chuyn n bng thuc tnh v nhn nt 3 chm (...) mc Insert chn thao tc tng ng:

    Khi nhn nt ny, ca s sau s hin ra c th ty bin hnh vi Insert:

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    86/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    86

    trn, nu bn chn ch mc nhin ("Use Runtime") th LINQ to SQL s tnh ton v sinh ra culnh SQL ng thc hin cc thao tc tng ng. Chng ta c th thay i bng cch nhn chutvo Customize v chn th tc InsertOrder t danh sch cc SPROC:

    LINQ to SQL s hin th cc tham s ca th tc m ta chn, v cho php nh x cc thuc tnhca lp Order v cc tham s ca InsertOrder. Mc nhin, LINQ to cng t ng xc nh cc thams tng ng theo tn, tuy nhin bn vn c th sa li nu mun.

  • 7/31/2019 LINQ to SQL-Tutorial-Can Ban

    87/103

    Su tm bi:www.daihoc.com.vn

    LINQ to SQL Tutorial

    87

    Nhn vo nt Ok l xong. Gi y bt c khi no mt i tng Order c thm vo DataContextv phng thc SubmitChanges() c gi, th tc InsertOrder s c thc thi thay cho cu lnhSQL ng.

    Quan trng:Mc d hin ti chng ta dng SPROC cp nht, phng thc "OnValidate" caOrder m chng ta to trc y (trong bc 1 ca bi vit ny) kim tra tnh hp l ca itng Order s vn c thc thi trc khi bt k thay i no c thc hin. Do vy chng ta sc mt cch r rng x l v kim tra cc quy tc, v c th dng li mt cch d dng m khng

    ph thuc vo vic chng ta dng SQL ng hay dng SPROC.

    14. Bc 4: Thc hin cp nht dng SPROCGi chng ta s sa li i tng Customer cho php cp nht bng cch dng SPROC.

    Chng ta s bt u bng cch to mt SPROC tn "UpdateCustomer" nh di y:

    Ch trn, ngoi vic truyn gi tr cho tham s CustomerID, ti cng truyn mt tham s khc ctn @Original_CustomerID. Ct CustomerID trong bng Customers khng phi l mt ct t tng,v n c th c chnh sa nh mt phn ca thao tc cp nht. Do vy chng ta s phi truyn c

    gi tr ca CustomerID c v CustomerID mi c th cp nht. Chng ta s xem cch nh x ccct ngay sau y.

    Bn s thy trn ti truyn mt tham s c tn @Version (c kiu timestamp) vo cho SPROC.y l mt ct ti thm vo bng Customers c th x l vic tranh chp khi cc thao tc cpnht c din ra ng thi (optimistic concurrency). Ti s ni chi tit hn v vic x l tranh chpny trong bi vit sau ca lot bi LINQ to SQL, nhng ti cng ni lun l LINQ to SQL h tr y optimistic concurrency, v cho php bn c t