Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
Using ADO .NET - More DB Updating - Part 5
3 - A Few Words About Design
 More of this Feature
• 1 - Changing Without Wizards
• 2 - ADO .NET: Built for Networks
• 4 - The CommandBuilder
• 5 - Autonumber Complications
• 6 - The Code and Download
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• Part 1 of the ADO .NET Series
• Part 2 of the ADO .NET Series
• Part 3 of the ADO .NET Series
• Part 4 of the ADO .NET Series
• VB .NET Books!
• New to VB .NET?
  Learn It Here!
 
 Elsewhere on the Web
• About.Com DATABASES
• MSDN Technical Article: CommandBuilder object
 

A few words about design are important here.

Each time the data in the form is changed and the button is clicked, a "round trip" of changing the data in the form and then permanently posting the change to the database is done. This may not be the best design for your application since you may want to accumulate more data 'on the client side' before reconnecting with a database to actually make these changes 'on the server side'. ADO .NET is designed to help you do this. This program is an example to illustrate what a round trip looks like in each case.

The first part of the program should be familiar to you since all we do is create a DataSet and populate properties in form components using the data in the DataSet. But before anything else, is coded, an Imports statement is added at the top to simplify coding ...

Imports System.Data.OleDb

and several global declarations are added since these objects will be used throughout the program. And the global variables artKeySave that we'll need for updating the database is here rather than passing it as a subroutine parameter.

Dim myOleDbDataAdapter As OleDbDataAdapter
Dim myDataSet As DataSet = New DataSet()
Dim myDataTable As DataTable = New DataTable()
Dim myOleDbConnection As OleDbConnection
Dim myCommand As OleDbCommandBuilder
Dim artKeySave As Integer

In the form Load event, the form is initialized and the artTitle and artKey database fields are bound to the ListBox control. (Note ... You must make sure that {path} in the connection object is changed to match your own file configuration.)

myOleDbConnection = New _
       OleDbConnection( _
       "Provider=Microsoft.Jet.OLEDB.4.0;" _
       & "User ID=Admin;Data Source={path}\AboutVB.mdb")
Try
    myOleDbDataAdapter = _
        New OleDbDataAdapter("select * from Articles", _
        myOleDbConnection)
    myOleDbDataAdapter.Fill(myDataTable)
    lstAboutVBData.DataSource = myDataTable
    lstAboutVBData.DisplayMember = "artTitle"
    lstAboutVBData.ValueMember = "artKey"
Catch
    Console.WriteLine("Error Opening {0}", _
        myOleDbConnection.DataSource)
End Try
myCommand = New OleDbCommandBuilder(myOleDbDataAdapter)

The ListBox properties are the same ones that were set using wizards and in the properties dialog, but this time they're being set at 'run time' under program control instead of at 'design time'. Notice also that we Fill a DataTable and use that as our DataSource. A DataTable is simply one part of a DataSet and also helps illustrate the flexibility of the new ADO .NET objects.

Next page > The CommandBuilder > Page 1, 2, 3, 4, 5, 6

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.