platform technology unit 4.ppt
TRANSCRIPT
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
1/27
2002 Prentice Hall. All rights reserved.
1
VB.NET
UNIT 4
For
III-CSE
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
2/27
2002 Prentice Hall. All rights reserved.
CONTENTS
DELEGATES AND EVENTS
ACCESSING DATA
ADO.NET OBJECT MODEL
.NET DATA PROVIDERS
DIRECT ACCESS TO DATA
ACESSING DATA WITH DATASETS
2
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
3/27
2002 Prentice Hall. All rights reserved.
3
1. Delegates
Delegates Classes that encapsulate a set of references to methods
A delegate object that contains method references can be
passed to another method
Singlecast delegates
Delegates containing a single method and they are created and
derived from class Delegate
Multicast delegates
Delegates containing multiple methods and they are derived
from class MulticastDelegate AddressOf
Creates a delegate instance enclosing a reference to that
method
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
4/27
2002 Prentice Hall.
All rights reserved.
Outline4
DelegateBubbleSort.vb
1 ' Fig. 10.24: DelegateBubbleSort.vb2 ' Uses delegates to sort random numbers (ascending or descending).34 Public ClassCDelegateBubbleSort56 ' delegate definition7 Public Delegate FunctionComparator( _
8 ByValelement1As Integer, _9 ByValelement2As Integer)As Boolean1011 ' sort array depending on comparator12 Public SubSortArray(ByValarrayAs Integer(), _13 ByValCompareAsComparator)1415 Dimi, passAs Integer1617 Forpass = 0Toarray.GetUpperBound(0)1819 ' comparison inner loop20 Fori = 0Toarray.GetUpperBound(0) - 12122 IfCompare(array(i), array(i + 1)) Then23 Swap(array(i), array(i + 1))24 End If
2526 Next' inner loop2728 Next' outer loop2930 End Sub' SortArray3132 ' swap two elements33 Private SubSwap(ByReffirstElementAs Integer, _
34 ByRefsecondElementAs Integer)35
Declaration of a delegate Comparator
Returns value Boolean
Define method SortArray
Determines how to sort the array
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
5/27
2002 Prentice Hall.
All rights reserved.
Outline5
DelegateBubbleSort.vb
36 DimholdAs Integer3738 hold = firstElement39 firstElement = secondElement40 secondElement = hold41 End Sub' Swap42
43 End Class' CDelegateBubbleSort
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
6/27
2002 Prentice Hall.
All rights reserved.
Outline6
FrmBubbleSort.vb
1 ' Fig. 10.25: FrmBubbleSort.vb2 ' Create GUI that enables user to sort array.34 ImportsSystem.Windows.Forms56 Public ClassCFrmBubbleSort7 InheritsForm
89 ' TextBox that contains original list10 Friend WithEventstxtOriginalAsTextBox11 Friend WithEventslblOriginalAsLabel1213 ' TextBox that contains sorted list14 Friend WithEventstxtSortedAsTextBox15 Friend WithEventslblSortedAsLabel1617 ' Buttons for creating and sorting lists18 Friend WithEventscmdCreateAsButton19 Friend WithEventscmdSortAscendingAsButton20 Friend WithEventscmdSortDescendingAsButton2122 ' Windows Form Designer generate code2324 ' reference to object containing delegate
25 DimmBubbleSortAs NewCDelegateBubbleSort()2627 ' original array with unsorted elements28 DimmElementArrayAs Integer() =NewInteger(9){}2930 ' delegate implementation sorts in asending order31 Private FunctionSortAscending(ByValelement1As Integer, _32 ByValelement2As Integer)As Boolean33
34 Returnelement1 > element235 End Function' SortAscending
Displays a list of unsorted numbers
Displays a list of sorted sequence
Creates a list in either ascending or descending order
Defines method SortAscending
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
7/27
2002 Prentice Hall.
All rights reserved.
Outline7
FrmBubbleSort.vb
3637 ' delegate implementation sorts in descending order38 Private FunctionSortDescending(ByValelement1As Integer, _39 ByValelement2As Integer)As Boolean4041 Returnelement1 < element242 End Function' SortDescending
4344 ' creates random generated numbers45 Private SubcmdCreate_Click(ByValsenderAsSystem.Object, _46 ByValeAsSystem.EventArgs) HandlescmdCreate.Click4748 txtSorted.Clear()4950 DimoutputAs String51 DimrandomNumberAsRandom =NewRandom()52 DimiAs Integer5354 ' create String with 10 random numbers55 Fori = 0TomElementArray.GetUpperBound(0)56 mElementArray(i) = randomNumber.Next(100)57 output &= mElementArray(i) & vbCrLf58 Next59
60 txtOriginal.Text = output ' display numbers6162 ' enable sort buttons63 cmdSortAscending.Enabled = True64 cmdSortDescending.Enabled = True65 End Sub' cmdCreate_Click66
Defines method SortDescending
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
8/27
2002 Prentice Hall.
All rights reserved.
Outline8
FrmBubbleSort.vb
67 ' display array contents in specified TextBox68 Private SubDisplayResults()6970 DimoutputAs String71 DimiAs Integer7273 ' create string with sorted numbers
74 Fori = 0TomElementArray.GetUpperBound(0)75 output &= mElementArray(i) & vbCrLf76 Next7778 txtSorted.Text = output ' display numbers79 EndSub' DisplayResults8081 ' sorts randomly generated numbers in ascending manner82 Private SubcmdSortAscending_Click(ByValsenderAs_83 System.Object, ByValeAsSystem.EventArgs) _84 HandlescmdSortAscending.Click8586 ' sort array87 mBubbleSort.SortArray(mElementArray,AddressOfSortAscending)8889 DisplayResults() ' display results90
91 cmdSortAscending.Enabled = False92 cmdSortDescending.Enabled = True93 EndSub' cmdSortAscending_Click9495 ' sorts randomly generated numbers in descending manner96 PrivateSubcmdSortDescending_Click(ByValsenderAs_97 System.Object, ByValeAsSystem.EventArgs) _98 HandlescmdSortDescending.Click99
100 ' create sort object and sort array101 mBubbleSort.SortArray(mElementArray,AddressOfSortDescending)
Methods cmdSortAscending_Clickand
cmdSortDescending_Clickare invoked when the user clicks the
Sort Ascendingand Sort Descendingbuttons
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
9/27
2002 Prentice Hall.
All rights reserved.
Outline9
FrmBubbleSort.vb
102103 DisplayResults() ' display results104105 cmdSortDescending.Enabled = False106 cmdSortAscending.Enabled = True107 End Sub' cmdSortDescending_Click108
109 End Class' CFrmBubbleSort
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
10/27
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
11/27
2002 Prentice Hall.
All rights reserved.
Outline11
FrmBubbleSort.vb
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
12/27
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
13/272002 Prentice Hall. All rights reserved.
13
12.3 Event-Handling Model
Fig. 12.5 Event-handling model using delegates.
Object A raises event EDelegate for event E
Handler 1 for event E
Handler 3 for event E
Handler 2 for event E
calls
calls
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
14/272002 Prentice Hall. All rights reserved.
14
12.3 Event-Handling Model
Fig. 12.6 Events section of the Properties window.
15
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
15/27
2002 Prentice Hall.
All rights reserved.
Outline15
SimpleEvenExample.vb
1 ' Fig. 12.7: SimpleEventExample.vb2 ' Program demonstrating simple event handler.34 PublicClassFrmSimple5 InheritsSystem.Windows.Forms.Form67 #Region " Windows Form Designer generated code "
89 PublicSubNew()10 MyBase.New()1112 ' This call is required by the Windows Form Designer.13 InitializeComponent()141516 ' Add any initialization after the17 ' InitializeComponent() call18 EndSub ' New1920 ' Form overrides dispose to clean up the component list.21 ProtectedOverloadsOverridesSubDispose( _22 ByValdisposingAsBoolean)2324 Ifdisposing Then
2526 IfNot(components IsNothing) Then27 components.Dispose()28 EndIf2930 EndIf3132 MyBase.Dispose(disposing)33 EndSub ' Dispose
3435 FriendWithEventslblOutputAsSystem.Windows.Forms.Label
Beginning of Visual Studio
generated code
16
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
16/27
2002 Prentice Hall.
All rights reserved.
Outline16
SimpleEvenExample.vb
3637 ' Required by the Windows Form Designer38 PrivatecomponentsAsSystem.ComponentModel.Container3940 ' NOTE: The following procedure is required by41 ' the Windows Form Designer.42 ' It can be modified using the Windows Form Designer.
43 ' Do not modify it using the code editor.44 _45 PrivateSubInitializeComponent()46 Me.lblOutput =NewSystem.Windows.Forms.Label()47 Me.SuspendLayout()48 '49 'lblOutput50 '51 Me.lblOutput.Location =NewSystem.Drawing.Point(32, 48)
52 Me.lblOutput.Name = "lblOutput"53 Me.lblOutput.Size =NewSystem.Drawing.Size(168, 40)54 Me.lblOutput.TabIndex = 055 Me.lblOutput.Text = "Click Me!"56 '57 'FrmSimple58 '59 Me.AutoScaleBaseSize =NewSystem.Drawing.Size(5, 13)
60 Me.ClientSize =NewSystem.Drawing.Size(272, 237)61 Me.Controls.AddRange( _62 NewSystem.Windows.Forms.Control() {Me.lblOutput})6364 Me.Name = "FrmSimple"65 Me.Text = "SimpleEventExample"66 Me.ResumeLayout(False)67 EndSub68
69 #End Region70
End of Visual Studio
generated code
17
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
17/27
2002 Prentice Hall.All rights reserved.
Outline17
SimpleEvenExample.vb
71 ' handler for click event on lblOutput72 PrivateSublblOutput_Click(ByValsenderAsObject, _73 ByValeAsSystem.EventArgs) HandleslblOutput.Click7475 MessageBox.Show("Label was pressed")76 EndSub ' lblOutput_Click77
78 EndClass' FrmSimpleExample
Name of the handler
lowed by an underscore
and the event name
Reference to the object
that generated the event
Event arguments object
Event-handling code
displays a message box
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
18/27
30
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
19/27
2002 Prentice Hall.All rights reserved.
Outline30
DisplayTable.vb
295296 Me.ResumeLayout(False)297298 EndSub' InitializeComponent299300 #EndRegion301
302 EndClass' FrmTableDisplay
31
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
20/272002 Prentice Hall. All rights reserved.
31
19.6.2 Querying the Books Database
Use SELECTstatements on database and displayresults
32
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
21/27
2002 Prentice Hall.All rights reserved.
Outline32
DisplayQueryResults.vb
1 ' Fig. 19.28: DisplayQueryResults.vb2 ' Displays the contents of the authors database.34 PublicClassFrmDisplayQueryResult5 InheritsSystem.Windows.Forms.Form67 FriendWithEventstxtQueryAsSystem.Windows.Forms.TextBox
8 FriendWithEventscmdSubmitAsSystem.Windows.Forms.Button9 FriendWithEventsdgdResultsAsSystem.Windows.Forms.DataGrid10 FriendWithEventsBooksConnectionAs_11 System.Data.OleDb.OleDbConnection1213 FriendWithEventsBooksDataAdapterAs_14 System.Data.OleDb.OleDbDataAdapter1516 FriendWithEventsBooksDataSetAsSystem.Data.DataSet
1718 ' Visual Studio .NET generated code1920 ' perform SQL query on data21 PrivateSubcmdSubmit_Click(ByValsenderAsSystem.Object, _22 ByValeAsSystem.EventArgs) HandlescmdSubmit.Click2324 Try2526 ' set the text of the SQL query to what the user typed27 ' in28 BooksDataAdapter.SelectCommand.CommandText = _29 txtQuery.Text3031 ' clear the DataSet from the previous operation32 BooksDataSet.Clear()33
FormFrmDisplayQueryResultcontainsTextBoxtxtQuery, in which users input SELECT
statements
After entering a query, the user clicks ButtoncmdSubmit,labeled SubmitQuery, to view the results of the query
33
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
22/27
2002 Prentice Hall.All rights reserved.
Outline33
DisplayQueryResults.vb
Program Output
34 ' Fill the data set with the information that results35 ' from the SQL query36 BooksDataAdapter.Fill(BooksDataSet, "Authors")3738 ' Bind the DataGrid to the contents of the DatSet39 dgdResults.SetDataBinding(BooksDataSet, "Authors")40
41 ' display database connection verbose message42 CatchexceptionAs System.Data.OleDb.OleDbException43 MessageBox.Show("Invalid Query")44 EndTry4546 EndSub' cmdSubmit_Click4748 EndClass' FrmDisplayQueryResults
34
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
23/27
2002 Prentice Hall. All rights reserved.
34
19.8 Reading and Writing XML Files
ADO.NET can convert data from data source intoXML files
Uses methods WriteXml, ReadXml andGetXml
35
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
24/27
2002 Prentice Hall.All rights reserved.
Outline35
XMLWriter.vb
1 ' Fig. 19.30 XMLWriter.vb2 ' Demonstrates generating XML from an ADO.NET DataSet34 PublicClassFrmXMLWriter5 InheritsSystem.Windows.Forms.Form67 ' constructor
8 PublicSubNew()9 MyBase.New()1011 ' This call is required by the Windows Form Designer.12 InitializeComponent()1314 ' Add any initialization after the15 ' InitializeComponent() call16
17 ' open database connection18 BaseballConnection.Open()1920 ' fill DataSet with data from OleDbDataAdapter21 BaseballDataAdapter.Fill(BaseballDataSet, "Players")2223 ' bind DataGrid to DataSet24 dgdPlayers.SetDataBinding(BaseballDataSet, "Players")25 End Sub2627 FriendWithEventscmdWriteAsSystem.Windows.Forms.Button28 FriendWithEventsdgdPlayersAsSystem.Windows.Forms.DataGrid29 FriendWithEventstxtOutputAsSystem.Windows.Forms.TextBox30 FriendWithEventsBaseballConnectionAs_31 System.Data.OleDb.OleDbConnection3233 FriendWithEventsBaseballDataAdapterAs_
34 System.Data.OleDb.OleDbDataAdapter35
Establishes a connection to the BaseballdatabaseMethod Fill of class OleDbDataAdapteris called topopulate BaseballDataSetwith data from thePlayerstable in the Baseballdatabase
Binds the dgdPlayersto BaseballDataSetto
display the information to the user
36
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
25/27
2002 Prentice Hall.All rights reserved.
Outline36
XMLWriter.vb
36 FriendWithEventsBaseballDataSetAsSystem.Data.DataSet3738 ' Visual Studio .NET generated code3940 ' write XML representation of DataSet when button clicked41 PrivateSubcmdWrite_Click(ByValsenderAsSystem.Object, _42 ByValeAsSystem.EventArgs) HandlescmdWrite.Click
4344 ' write XML representation of DataSet to file45 BaseballDataSet.WriteXml("Players.xml")4647 ' display XML in TextBox48 txtOutput.Text &= "Writing the following XML:"& _49 vbCrLf& BaseballDataSet.GetXml() & vbCrLf50 EndSub' cmWrite_Click51
52 EndClass' FrmXMLWriter
DataSetmethodWriteXmlis invoked to generate anXML representation of the data contained in the DataSet
and then writes the XML to the specified file
37
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
26/27
2002 Prentice Hall. All rights reserved.
37
19.8 Reading and Writing XML Documents
1
2
3
4 John
5 Doe
6 0.375
7 1
8
9
10
11 Jack
12 Smith
13 0.223
14 2
15
16
17
18 George
19 O'Malley
20 0.44421 3
22
23
Fig. 19.31 XML document generated from DataSetin DatabaseXMLWriter.
38
-
8/13/2019 PLATFORM TECHNOLOGY UNIT 4.ppt
27/27
.NET DATA PROVIDERS
38