Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of

Wrapped DataSet Code Examples

As usual, we are going to take a look at some code samples, both from the server side and from the client side. First we'll look at some from the server side. In Listing 1, you find some code for when data is fetched from the database with the help of a stored procedure.

Listing 1: Code for Filling a DataSet

Dim aCommand As New SqlCommand (SprocOrder_FetchWithLines, [ccc]
GetClosedConnection)
aCommand.CommandType = _CommandType.StoredProcedure
aCommand.Parameters.Add _("@id", SqlDbType.Int).Value = id

    Dim anAdapter As New SqlDataAdapter(aCommand)

    anAdapter.TableMappings.Add("Table", "Orders")
    anAdapter.TableMappings.Add _
  ("Table1", "OrderLines")

  anAdapter.TableMappings _  (OrderTables.Orders).[ccc]
ColumnMappings.Add _
  ("Customer_Id", "CustomerId")
    anAdapter.TableMappings _
  (OrderTables.OrderLines).ColumnMappings.Add _
     ("Orders_Id", "OrderId")
    anAdapter.TableMappings _
  (OrderTables.Orders).ColumnMappings.Add _
     ("Product_Id", "ProductId")

    anAdapter.Fill(dataSet)

NOTE

It's less important what names you use for the DataTables and Columns when you wrap the DataSet and thereby hide the names. I changed the name in Listing 1 because I was "lazy" and reused the same routine for filling a DataSet as I did for untyped and typed DataSets from Part 2.

The filled DataSet is then sent to the constructor of the wrapper class, as you can see in Listing 2.

Listing 2: Sending the Filled DataSet to the Constructor of the Wrapper Class

    Dim anOrder As New OrderWrap(anOrderDataSet)

NOTE

I know, using a DataSet in the constructor violates encapsulation. This is a simple solution for my tests, but in a real app you should definitely guard encapsulation better.

And now some code from the client side. To browse the information in the wrapped class, the code could look like it does in Listing 3. Note that here I'm browsing both an order and all its order lines, as usual. (Hopefully you find it useful to see the "same" code for all the different data container options so that you can compare the code side by side.)

Listing 3: Code for Browsing a Wrapped DataSet

Dim anOrder As OrderWrap = _service.FetchOrderAndLines[ccc]
(_GetRandomId())

    _id = anOrder.Id
    _customerId = anOrder.CustomerId
    _orderDate = anOrder.OrderDate

    Dim i As Integer
    For i = 0 To anOrder.NoOfLines - 1
      _productId = anOrder.ProductId(i)
      _priceForEach = anOrder.PriceForEach(i)
      _noOfItems = anOrder.NoOfItems(i)
      _comment = anOrder.Comment(i)
    Next

Listing 3 has very little and compact code, and I think it's pretty clear. As you saw, my solution was to have a property that returns the number of order lines (NoOfLines). That is used for finding the upper bound to be used in an ordinary For index loop.

Another solution—and a more appealing one—would be to hand out an IEnumerator for the order lines, but here I'm concerned about creating something that is good enough for solving the problem of creating something testable.

Now let's turn to the server-side code for the Hashtable.

  • + Share This
  • 🔖 Save To Your Account