13 C# .NET Databases

Embed Size (px)

Citation preview

  • 8/13/2019 13 C# .NET Databases

    1/36

    SQL Server Express and C# .NET

    In this section, you'll learn how to create a database with SQL Server Express. You cando all this within the Visual C .!E" so#tware. $nce you have a database, you'll learn

    how to pull records #ro% it, and display the% on a &indows or%. You will also learn

    how to navi(ate throu(h the records in your database, and how to add new records.

    What is SQL Server Express?

    SQL Server Express is database syste% #ro% )icroso#t. It's the stripped down version o#

    SQL Server, which is used by a lot o# bi( businesses around the world. ortunately,)icroso#t have %ade the Express Edition a #ree download.

    *ope#ully, you downloaded SQL Server Express with the Visual C .!E" Express

    Edition. I# you did, you should see this so%ewhere on your start %enu+

    I# you can't see the SQL Server Express entry, then you %ay need to download it. t the

    ti%e o# writin(, the download location is here on the )icroso#t site+

    http+--www.%icroso#t.co%-slserver-en-us-editions-/01/2editions-express.aspx

    "he SQL Server )ana(e%ent Studio Express you can see in the i%a(e above is a

    separate download, and is piece o# so#tware that lets you create and %ana(e your

    databases without startin( up your Visual C .!E" so#tware. You don't need to downloadit #or this boo3. 4ut i# you want to (rab it, (o to the )icroso#t site and type 5SQL Server

    )ana(e%ent Studio Express5 into the search box.

    $nce you have downloaded and installed SQL Server Express, %a3e sure it's up and

    runnin(. Clic3 the Configuration Tools%enu above then the SQL Server

    Configuration Managerite%. You should see a window appear+

    1

    http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/express.aspxhttp://www.microsoft.com/sqlserver/en/us/editions/2012-editions/express.aspx
  • 8/13/2019 13 C# .NET Databases

    2/36

    In the ri(ht pane, %a3e sure the State #or both says 6unnin(. I# not, ri(ht clic3 and selectStart #ro% the %enu. You can then close the Con#i(uration )ana(er.

    Ho to Create a Co!pa"t SQL Server ata$ase

    in C# .NET

    Start a new pro7ect, and call it anythin( you li3e as we're only usin( this pro7ect to createa database. ro% the %enu at the top, clic3 on %ro&e"t ' (dd Ne )te!.

    ro% the dd !ew Ite% dialo(ue box in version /008, loo3 #or the Lo"al

    ata$aseite%, as in the i%a(e below+

    /

  • 8/13/2019 13 C# .NET Databases

    3/36

    In C /010, you'll see this dialo(ue box 9the /01/ version is si%ilar only less colour#ul:+

    ;

  • 8/13/2019 13 C# .NET Databases

    4/36

    (ain, select Local

  • 8/13/2019 13 C# .NET Databases

    5/36

    ?ut a tic3 in the box next to "ables, and then chan(e the na%e o# the dataset to this+

    ds+e!plo*ees

    In version /010 and /01/ o# the so#tware, you'll see this instead+

    @

  • 8/13/2019 13 C# .NET Databases

    6/36

    Select

  • 8/13/2019 13 C# .NET Databases

    7/36

    (ain, chan(e the na%e o# the

  • 8/13/2019 13 C# .NET Databases

    8/36

    In the next part below, you'll learn how to create tables in your SQL Server

  • 8/13/2019 13 C# .NET Databases

    9/36

    &hen you clic3 Create Ta$le, you'll see a dialo(ue box appear. "his one+

    "his screen is where you set up the Colu%ns that (o in the table. You'll (et to enter theactual data later. 4ut you need to tell SQL Server what 3ind o# data 9ata T*pe: is (oin(into each colu%n.

    irst, thou(h, type a na%e #or your table in the Na!ebox at the top. Call the

    table t$l+e!plo*ees.

  • 8/13/2019 13 C# .NET Databases

    10/36

  • 8/13/2019 13 C# .NET Databases

    11/36

    sin( the sa%e techniue as above, create the #ollowin( Colu%n !a%es,

  • 8/13/2019 13 C# .NET Databases

    12/36

    (dding ata to a SQL Server ata$ase Ta$le

    "o add entries to your SQL Server database, ri(ht clic3 on your table na%e in the

  • 8/13/2019 13 C# .NET Databases

    13/36

    "he reason it does this is because we set )s )dentit*to True, and the)dentit*

    )n"re!entto 1 2 it's an uto Incre%ent #ield, in other words.

    4ut we have now created one row in our database table. ill out a #ew %ore rows. Youcan use the sa%e details as ours, in the i%a(e below+

  • 8/13/2019 13 C# .NET Databases

    14/36

    "o #ind your database, have a loo3 in your Visual Studio ?ro7ects #older, usually located

    in theo"u!ents#older. ind the #older with the na%e o# the ?ro7ect you 7ust closed

    down. You should see the E!plo*ees.sdf#ile. I# you can't #ind it that way, do a searchthrou(h the &indows Start %enu.

    $nce you've #ound the database, copy the S

  • 8/13/2019 13 C# .NET Databases

    15/36

    $n the le#t hand side, clic3 on the Extensionsite%, you'll then see a list o# ob7ects you

    can add to your pro7ect. Scroll down to near the botto% and you'll see an ite%

    #or S*ste!.ata.S/lServerCe. You %ay see two o# these, a later version and an earlierversion. *old your %ouse over the latest version and you'll see a chec3box appear. ?ut a

    chec3 in the chec3box and then clic3 $D. You will now be able to use

    the S/lServerCeob7ects.

    "o set up a S/lServerCeconnection variable, double clic3 the blan3 #or%. ust outside o#the or% Load event add the #ollowin(+

    S*ste!.ata.S/lServerCe.S/lCeConne"tion "on2

    &hen you type a dot a#ter S/lServerCeyou'll see lots o# SlCe ob7ects appear. &e're

    usin( theS/lCeConne"tionob7ect and (ivin( it the na%e con.

    Inside o# the or% Load event, we can create a new con ob7ect. dd the #ollowin( line+

    "on 3 ne S*ste!.ata.S/lServerCe.S/lCeConne"tion452

    connection ob7ect needs a connection strin(. "his points the connection ob7ect to where

    your database is. dd the #ollowin( line to your code+

    "on.Conne"tionString 3 6ata

    Sour"e3C788sers889ner88o"u!ents88E!plo*ees.sdf62

    Conne"tionStringis a ?roperty o# connection ob7ects. #ter an eual si(n, you type the

    path to your database #ile. "he path needs to (o a#ter the text 5

  • 8/13/2019 13 C# .NET Databases

    16/36

    $nce you've added the lines above, you code should loo3 li3e this+

    6un your pro(ra%%e and you should see the %essa(e box appear when you clic3 your

    button.

    Stop your pro(ra%%e and return to your code. You can delete or co%%ent out the%essa(e box.

    In th enext section below, we'll ta3e a loo3 at

  • 8/13/2019 13 C# .NET Databases

    17/36

    or the

  • 8/13/2019 13 C# .NET Databases

    18/36

    "o #ill the dataset with records #ro% the database, you use the ers6 52

    &hat this does is to ,illa

  • 8/13/2019 13 C# .NET Databases

    19/36

    etting at the data in the ataset

    t the %o%ent, we have a ers6A.1os@BA2

    1

  • 8/13/2019 13 C# .NET Databases

    20/36

    You #irst type the na%e o# your

  • 8/13/2019 13 C# .NET Databases

    21/36

    4ut you are al%ost ready to test it all out. "he #inal thin( to do is to add a call to your

    !avi(ate6ecords %ethod. ?ut the call 7ust be#ore the "on.Closeline, as in the i%a(e

    below+

    !ow you can test it out. 6un your pro(ra%%e and the #or% should display the #irst record#ro% your database. It should loo3 li3e ours+

    !ow that we have one record displayed, we can add buttons to navi(ate bac3wards and#orward throu(h all the records in our database. &e'lldo that in the next part below.

    /1

  • 8/13/2019 13 C# .NET Databases

    22/36

    S"rolling through the data$ase

    "he #irst thin( we'll do is to allow users to %ove #orward throu(h each record in our

    database. "his is done with 7ust a bit o# pro(ra%%in( lo(ic, and %anipulatin( the 6owvalue in the

  • 8/13/2019 13 C# .NET Databases

    23/36

    4ut this will point to 6ow0M all the ti%e. &e can use the in"variable here. &hat we'll do

    is to incre%ent the value when the !ext 6ecord button is clic3ed, addin( 1 to inc every

    ti%e.

    Chan(e the line to this+

    ata1o d1o 3 ds;.Ta$les@6Wor>ers6A.1os@incA2

    "he only chan(e is in between the suare brac3ets o# 6ows.

    6un your pro(ra%%e to test i# it wor3s. You should still see the #irst record displayed in

    your text boxes.

    Stop your pro(ra%%e and return to the desi(n environ%ent. dd a button to your #or%.Chan(e the "ext property to !ext 6ecord. Chan(e the !a%e property to $tnNext.

  • 8/13/2019 13 C# .NET Databases

    24/36

    Move :a">ards through the ata$ase

    &e can use si%ilar code to %ove bac3wards throu(h the records in the database.dd another button to your #or%. Chan(e the "ext property to ?revious 6ecord. Chan(e

    the !a%e property to btn?revious.

  • 8/13/2019 13 C# .NET Databases

    25/36

    Clic3 both o# your buttons and %a3e sure you can %ove bac3 and #orward throu(h therecords. You pro(ra%%e shouldn't crashF

    Gu!p to the Last 1e"ord in *our ata$ase

    "o %ove to the last record o# your database, you only need to %a3e sure that the incvariable and )ax6ows have the sa%e value.

    dd a new button to your #or%. Set the "ext property as Last 6ecord, and the !a%e

    property as btnLast.

  • 8/13/2019 13 C# .NET Databases

    26/36

    dd another button to your #or%. Chan(e the "ext property to irst 6ecord. Chan(e the

    !a%e property to btnirst.

  • 8/13/2019 13 C# .NET Databases

    27/36

    #ter a new record has been entered into the text boxes, we can Save it. So double clic3

    your Save button to (et at the code.

    "o save a record, you need to do two thin(s+ save it to the

  • 8/13/2019 13 C# .NET Databases

    28/36

    !otice the last two lines+

    Max1os 3 Max1os ;2

    in" 3 Max1os D ;2

    4ecause we have added a new 6ow to the

  • 8/13/2019 13 C# .NET Databases

    29/36

    Your code #or the Save button, then, should loo3 li3e this+

    !otice that we've disabled the Save button but enabled the dd!ew button. &e did the

    reverse with the dd!ew button+

    text:ox;.Clear452

    text:oxI.Clear452

    text:oxJ.Clear452text:oxK.Clear452

    $tn(ddNe.Ena$led 3 false2

    $tnSave.Ena$led 3 true2

    You can try your pro(ra%%e out, now. Clic3 your (dd Nebutton and add a new

    record. Clic3 theSavebutton. &hen you close the pro(ra%%e down and reopen it, thenew record should be there.

    pdate a 1e"ord

    So%eti%es, all you want to do is to update a record in the database. "his is very si%ilar to

    ddin( a new record. Exa%ine the #ollowin( code+

    /

  • 8/13/2019 13 C# .NET Databases

    30/36

    "he #irst thin( to notice is that we've set up a %ethod called pdate:. "his has all the

    code #or the Co%%and 4uilder and is doin( the actual updatin(. &e can then re%ove thissa%e code #ro% the Save button+

    !otice the call to our new %ethod+

    pdate:4 52

    ;0

  • 8/13/2019 13 C# .NET Databases

    31/36

    or the pdate button, the #irst line o# the code is this+

    3 ds;.Ta$les@6Wor>ers6A.1os@in"A2

    "he only thin( you're not doin( is addin( a new 6ow. #ter creatin( a new 6ow called

    d6ow/, we set it to the current 6ow, usin( our in"variable. &hatever is in the text boxesthen (ets trans#erred to d6ow/1M, d6ow//M, d6ow/;M and d6ow/>M. "hese are the

    Colu%ns in the 6ow.

    &hen you run your #or% a(ain, a%end one o# your records. Close down the #or% and

    open it bac3 up a(ain. You should #ind that your a%end%ents are still there.

    elete a 1e"ord

    "o delete a record #ro% the

  • 8/13/2019 13 C# .NET Databases

    32/36

    I# you loo3 at the botto%, you'll see a label that says 56ecord 1 o# 105. I%ple%ent this in

    your own pro(ra%%e. I# you set up a %ethod, you can 7ust call it when the #or% loads,

    and a(ain #ro% !avi(ate6ecords.

    ,ind a 1e"ord

    use#ul #eature to add is a ind button. &hen a #ind button is clic3ed, you then displaythe record that the user was searchin( #or. $r display a 5!ot #ound5 %essa(e i# there were

    no %atchin( records.

    So add a new button to your #or%. Set the "ext property to ,ind, and the !a%e property

    to $tn,ind.

  • 8/13/2019 13 C# .NET Databases

    33/36

    string sear"h,or 3 6(ta62

    int results 3 B2

    ata1o@A returned1os2

    "he #irst variable sets up a strin( called sear"h,or. "his is obviously the record we want

    to #ind. &e've hard2coded the value, here, and 7ust entered a last na%e #ro% our databasetable. 4ut you'd want this value to co%e #ro% a text box on your #or%.

    "he second variable, results, will be used to tell us whether or not any results were #ound.

    "he third line is a

  • 8/13/2019 13 C# .NET Databases

    34/36

    &e're usin( a variable na%e 9sear"h,or:, instead.

    "he Select %ethod allows you to use other SQL 3eywords. I# you don't want an exact

    search, #or exa%ple, you can use Li3e instead o# J.

    Sele"t46last+na!e Li>e (ta65

    !ote where the sin(le uotes are 2 surroundin( the text you want to search #or. 4ecause

    our search used a variable, we're usin( plus sy%bols to concatenate. &hich is why it's so

    %essyF

    I# a row is #ound, it will then be stored in the returned1osarray. "o (et a count o# how%any rows were #ound, we can used this code+

    results 3 returned1os.Length2

    "his 7ust uses the Len(th property o# the returned1osarray. "he len(th is how %anyite%s are in the array. I# it's (reater than =ero, it %eans we've #ound a %atch. &e can use

    an i# state%ent to chec3+

    if 4results ' B5

    1EC91 ,9N

    F

    else

    Message:ox.Sho46No su"h 1e"ord652

    F

    I# a record is #ound, we need to (et at the values in the Colu%ns. &e can create a new

    6ow #or this+

    if 4results ' B5

    ata1o dr;2

    dr; 3 returned1os@BA2

    F

    ;>

  • 8/13/2019 13 C# .NET Databases

    35/36

    &e now set up a

  • 8/13/2019 13 C# .NET Databases

    36/36

    Close your pro(ra%%e down. Chan(e the na%e o# the person bein( searched #or and try

    a(ain.

    Exer"ise Q

    dd a text box to your #or%. Het the na%e o# the person #ro% this text box, rather thanusin( the hard coded value that you have at the %o%ent.

    nswer to Exercise Q

    $D, that's enou(h o# databasesF It's a hu(e sub7ect, obviously, and %any boo3s have been

    written on the sub7ect. &e've only touched the sur#ace in these lessons, and encoura(e

    you to delve deeper. Especially i# you want a 7ob as a pro(ra%%erF

    In the next section, we'll ta3e a loo3 at %ultiple #or%s.

    http://www.homeandlearn.co.uk/csharp/csharp_answers.html#exQhttp://www.homeandlearn.co.uk/csharp/csharp_answers.html#exQ