Workshop

for RuBoard

This workshop will help reinforce the concepts covered in today's lesson.

Quiz

1:

Why can't you pass a data reader by value between application domains?

A1:

Data reader classes are derived from MarshalByRefObject , which can be referenced from remote application domains but not serialized and copied between them. As a result, you can pass a data reader by reference but not by value. However, passing it by reference means that each time a method or property of the data reader is accessed from the remote domain, a call to the hosting domain must be made and the results returned. This results in unnecessary overhead.

2:

When would you use GetValues instead of methods such as GetString , GetInt32 , and GetByte ?

A2:

The GetValues method retrieves all the columns in the row and places them in an array of type System.Object . This is more efficient than reading each column individually, although to use the data with a strong type, you then need to access the element of the array and cast it to the appropriate type.

3:

How can I retrieve a large binary value with a data reader?

A3:

For large binary values, you would typically want to call the GetBytes method on the column repeatedly, each time retrieving a specific amount of data and placing it in a buffer (an array of bytes).

4:

Why would you declare a parameter as IDataReader rather than as SqlDataReader or OleDbDataReader ?

A4:

Creating methods that accept parameters or variables declared as the IDataReader interface rather than a derived type allows your code to work with any .NET Data Provider. Two specific examples include writing code in an ASP.NET page that casts the data reader returned from a data access class into IDataReader and then binds the data reader to a DataGrid , and writing methods that manipulate any object that implements the IDataReader interface.

Exercise

Q1:

Write a method that uses a SqlDataReader to save the cover images of all titles for which images exist in the database. (Note that the images are JPEG and are all the same cover.)

A1:

One possible solution might be

 Private Sub SavePhotos(ByVal connect As String)   Dim con As New SqlConnection(connect)   Dim com As New SqlCommand( _     "SELECT ISBN, cover FROM Titles WHERE cover IS NOT NULL", con)   Dim dr As SqlDataReader   Dim isbn As String   Dim cover() As Byte   Try     con.Open()     dr = com.ExecuteReader(CommandBehavior.CloseConnection)     Do While dr.Read       isbn = Trim(dr.GetString(0))       cover = CType(dr.GetValue(1), Byte())       ' Now write out the file       Dim fs As New FileStream(isbn & ".jpg", FileMode.OpenOrCreate)       Dim br As New BinaryWriter(fs)       br.Write(cover)       br.Close()       fs.Close()     Loop   Catch e As Exception     ' Handle the error     Console.WriteLine(e.Message)   Finally     dr.Close()   End Try End Sub 
for RuBoard


Sams Teach Yourself Ado. Net in 21 Days
Sams Teach Yourself ADO.NET in 21 Days
ISBN: 0672323869
EAN: 2147483647
Year: 2002
Pages: 158
Authors: Dan Fox

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net