Serializing SQL Server Data

Team Fly 

Page 81

You can exploit this information to include additional information with (or exclude information from) the object you serialize, depending on its context. If the object is being serialized to a file, for example, you can include a date/time property to indicate when the object was serialized. Upon deserialization you can extract this value and set the object's ''age." The following constructor serializes an object differently if the serialization process's destination is a file:

 Public Sub New (ByVal info As SerializationInfo, _                 ByVal context As StreamingContext)      If context.State = StreamingContextStates.File Then         ' Serialize a date//time member     Else         ' Serialize all other fields     End If End Sub 

Serializing SQL Server Data

Before we end this chapter, we'd like to discuss an interesting topic that combines SQL Server's support for XML and serialization. SQL Server can return the results of a query in XML format. The XML document describing the result of a query corresponds to the serialized version of a custom object and, if we can create a class that matches the schema of the XML document, we'll be able to deserialize SQL Server's XML response into an instance of the custom class. In this section we'll describe a technique for moving data out of SQL Server and into an instance of a custom class, without setting up DataAdapters and populating DataSets. The custom object will be an object that represents an order, including its header and its detail lines. The advantage of this approach, as compared to a straight ADO.NET approach based on DataSets, is that you don't have to worry about related tables and accessing related rows in DataTable objects. The custom object that represents the order is a business object that represents one of the entities you work with in your code, and you can manipulate it through its properties (read the values of an existing order, or create a new order). The application we'll use to demonstrate this technique is the NWOrders project, which reads existing orders from the Northwind database and creates new ones using a custom business object. The application's form is shown in Figure 3.4.

image

FIGURE 3.4 The NWOrders project

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

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