Microsoft word c# coban noidung

Click here to load reader

  • date post

    11-Mar-2016
  • Category

    Documents

  • view

    220
  • download

    3

Embed Size (px)

description

 

Transcript of Microsoft word c# coban noidung

  • Lu hnh ni b

  • Lu hnh ni b

    MC LC

    Chng 1. C bn ngn ng C# ............................................................................................ 1 I. Gii thiu ngn ng C# 2008 ...................................................................................... 1 II. Mi trng lp trnh ..................................................................................................... 2 III. Bin, hng, ton t ....................................................................................................... 3 IV. Quy c lp trnh, ng dng Console Application trong Visual Studio 2008 ............. 5 V. Kiu d liu ................................................................................................................. 9 VI. Cu trc iu khin .................................................................................................... 10 VII. Cu trc lp ................................................................................................................ 12

    Chng 2. Xy dng Windows Forms Application ........................................................... 15 I. S dng Visual Studio 2008 ...................................................................................... 15 II. Cc control c bn: Label, TextBox, Button, CheckBox, .................................... 18 III. Menu v ToolBar ....................................................................................................... 30 IV. Common Dialog ......................................................................................................... 30

    Chng 3. Array String Exception ............................................................................... 34 I. Mng 1 chiu ............................................................................................................. 34 II. Mng nhiu chiu ....................................................................................................... 37 III. String .......................................................................................................................... 40 IV. Exception ................................................................................................................... 45

    Chng 4. Class Object - Method .................................................................................... 47 I. Khi nim ................................................................................................................... 47 II. nh ngha lp (Class) ............................................................................................... 47 III. Phng thc (Method) ............................................................................................... 50

    Chng 5. SQL Server 2008 ................................................................................................ 54 I. Tng quan v SQL ..................................................................................................... 54 II. Tng quan v CSDL quan h ..................................................................................... 55 III. Table (Bng) .............................................................................................................. 58 IV. Cu lnh truy vn ....................................................................................................... 66 V. Mt s hm thng dng trong SQL Server ............................................................. 70

    Chng 6. Lp trnh kt ni CSDL SQL Server 2008 ...................................................... 72 I. To kt ni Vn chuyn d liu. ............................................................................ 72 II. S dng control .......................................................................................................... 73 III. Cc thao tc trn d liu: Thm - Sa - Xa vi ADO.NET ..................................... 78

    Chng 7. Xy dng ng dng ............................................................................................ 85 I. Chun b. .................................................................................................................... 85 II. S dng control .......................................................................................................... 85 III. S dng database ....................................................................................................... 88

  • Lu hnh ni b Trang 1

    Chng 1: C BN NGN NG C#

    Bi 1: GII THIU C# 2008

    I. Gii thiu C# Ngn ng C# kh n gin, ch khong hn 80 t kha v hn mi my kiu d liu

    c dng sn. Tuy nhin, ngn ng C# c ngha to ln khi n thc thi nhng khi nim lp trnh hin i. C# bao gm tt c nhng h tr cho cu trc, thnh phn component, lp trnh hng i tng. Nhng tnh cht hin din trong mt ngn ng lp trnh hin i. Hn na ngn ng C# c xy dng trn nn tng hai ngn ng mnh nht l C++ v Java.

    Tm li, C# c cc c trng sau y: - C# l ngn ng n gin - C# l ngn ng hin i - C# l ngn ng hng i tng - C# l ngn ng mnh m v mm do - C# l ngn ng hng module - C# s tr nn ph bin 1. C# l ngn ng n gin - C# loi b c mt vi s phc tp v ri rm ca cc ngn ng C++ v Java. - C# kh ging C / C++ v din mo, c php, biu thc, ton t. - Cc chc nng ca C# c ly trc tip t ngn ng C / C++ nhng c ci tin

    lm cho ngn ng n gin hn. 2. C# l ngn ng hin i C# c c nhng c tnh ca ngn ng hin i nh: - X l ngoi l - Thu gom b nh t ng - C nhng kiu d liu m rng - Bo mt m ngun 3. C# l ngn ng hng i tng C# h tr tt c nhng c tnh ca ngn ng hng i tng l: - S ng gi (encapsulation) - S k tha (inheritance) - a hnh (polymorphism) 4. C# l ngn ng mnh m v mm do - Vi ngn ng C#, chng ta ch b gii hn chnh bn thn ca chng ta. Ngn ng ny

    khng t ra nhng rng buc ln nhng vic c th lm. - C# c s dng cho nhiu d n khc nhau nh: to ra ng dng x l vn bn, ng

    dng ha, x l bng tnh; thm ch to ra nhng trnh bin dch cho cc ngn ng khc. - C# l ngn ng s dng gii hn nhng t kha. Phn ln cc t kha dng m t

    thng tin, nhng khng g th m C# km phn mnh m. Chng ta c th tm thy rng ngn ng ny c th c s dng lm bt c nhim v no.

    5. C# l ngn ng hng module - M ngun ca C# c vit trong Class (lp). Nhng Class ny cha cc Method

    (phng thc) thnh vin ca n. - Class (lp) v cc Method (phng thc) thnh vin ca n c th c s dng li

    trong nhng ng dng hay chng trnh khc. 6. C# s tr nn ph bin

  • Lu hnh ni b Trang 2

    C# mang n sc mnh ca C++ cng vi s d dng ca ngn ng Visual Basic. II. Mi trng lp trnh

    1. S dng Notepad son tho Bc 1: Son tho tp tin v lu vi tn C:\ChaoMung.cs c ni dung nh sau

    class ChaoMung {

    static void Main() {

    // Xuat ra man hinh chuoi thong bao 'Chao mung ban den voi C# 2008 ' System.Console.WriteLine("Chao mung ban den voi C# 2008 ") ; System.Console.ReadLine() ;

    } } Bc 2: Vo menu Start | All Programs | Microsoft Visual Studio 2008 | Visual

    Studio Tools | Visual Studio 2008 Command Prompt Bc 3:

    - G lnh bin dch tp tin ChaoMung.cs sang tp tin ChaoMung.exe C:\> csc /t:exe /out:chaomung.exe chaomung.cs

    - Chy tp tin ChaoMung.exe v c kt qu nh sau : C:\> chaomung.exe Chao mung ban den voi C# 2008

    2. S dng Micosoft Visual Studio 2008 to chng trnh Bc 1: Khi ng Visual Studio 2008

    Start | All Programs | Microsoft Visual Studio 2008 | Microsoft Visual Studio 2008 Bc 2: Vo menu File | New | Project Bc 3: Khai bo

  • Lu hnh ni b Trang 3

    * Mc nh: Visual Studio 2008 (Visual Studio .NET) s to ra tp tin Program.cs cha mt namespace tn ChaoMung v trong namespace ny cha mt class tn Program.

    Bc 4: trong phng thc Main, g on m lnh sau * V d:

    // Xuat ra man hinh chuoi thong bao 'Chao mung ban den voi C# 2008 ' System.Console.WriteLine("Chao mung ban den voi C# 2008 ") ; System.Console.ReadLine() ;

    Bc 5: chy chng trnh, nhn F5 hoc nhp vo nt

    III. Bin, hng, ton t 1. Bin a) Khi nim: - Bin l mt vng lu tr ng vi mt kiu d liu. - Bin c th c gn gi tr v cng c th thay i gi tr trong khi thc hin cc lnh

    ca chng trnh. b) Khai bo bin: Sau khi khai bo bin phi gn gi tr cho bin

    [ = ] ; c) V d 1.1: Khi to v gn gi tr mt bin

    class Bien {

    static void Main() {

    // Khai bao va khoi tao bien int bien = 9 ; System.Console.WriteLine("Sau khi khoi tao: bien = {0}", bien) ; // Gan gia tri cho bien bien = 5 ; // Xuat ra man hinh System.Console.WriteLine("Sau khi gan: bien = {0}", bien) ;

    } }

    2. Hng a) Khi nim: - Hng cng l mt bin nhng gi tr ca hng khng thay i trong khi thc hin cc

    lnh ca chng trnh. - Hng c phn lm 3 loi:

    + Gi tr hng (literal) + Biu tng hng (symbolic constants) + Kiu lit k (enumerations)

    b) Gi tr hng: V d: x = 100; // 100 c gi l gi tr hng

    c) Biu tng hng: gn mt tn hng cho mt gi tr hng. Khai bo: = ; V d 1.2: Nhp vo bn knh, in ra chu vi v din tch hnh trn.

  • Lu hnh ni b Trang 4

    class HinhTron {

    static void Main() {

    // Khai bao bieu tuong hang const double PI = 3.14159 ; // Khai bao bien int bankinh ; double chuvi , dientich ; string chuoi ; // Nhap gia tri cho bien chuoi System.Console.Write("Nhap ban kinh hinh tron: ") ; chuoi = System.Console.ReadLine() ; // Doi chuoi thanh so va gan vao bien so bankinh = System.Convert.ToInt32(chuoi) ; // Gan gia tri cho bien chuvi = 2 * bankinh * PI ; dientich = bankinh * bankinh * PI ; // Xuat ra man hinh System.Console.WriteLine("Chu vi hinh tron = {0:0.00}", chuvi) ; System.Console.WriteLine("Dien tich hinh tron = {0:0.00}", dientich) ;

    } }

    d) Kiu lit k: l tp hp cc tn hng c gi tr s khng thay i (danh sch lit k) Khai bo: {

    = , = , ... ,

    } ; V d:

    enum NhietDoNuoc {

    DoDong = 0, DoNguoi = 20, DoAm = 40, DoNong = 60, DoSoi = 100,

    } ; 3. Ton t a) Ton t ton hc: + , - , * , / , % b) Ton t tng / gim: += , -= , *= , /= , %= c) Ton t tng / gim 1 n v: ++ , -- d) Ton t gn: = e) Ton t quan h: == , != , > , >= , < ,