30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19 Database, SQL and ADO .<strong>NET</strong> 923<br />

<strong>to</strong> be displayed (line 47)—a table name, in this case. Readers can try entering their own queries<br />

in the text box and then pressing the Submit Query but<strong>to</strong>n <strong>to</strong> execute the query.<br />

Fig. 19.28 displays the output for FrmDisplayQueryResults. The first screenshot<br />

demonstrates the query results of retrieving all records from the Authors table. As<br />

the second screen capture demonstrates, clicking any column sorts the rows according <strong>to</strong><br />

the contents of that column in either ascending or descending order.<br />

19.7 <strong>Program</strong>ming with ADO .<strong>NET</strong>: Modifying a DBMS<br />

Our next example implements a simple address-book application that enables users <strong>to</strong> insert<br />

records in<strong>to</strong>, locate records from and update the Microsoft Access database<br />

Addressbook.<br />

The Addressbook application (Fig. 19.29) provides a GUI through which users can<br />

execute SQL statements on the database. Earlier in the chapter, we presented examples<br />

explaining the use of SELECT statements <strong>to</strong> query a database. This example provides that<br />

same functionality.<br />

Event handler cmdFind_Click (lines 72–119) performs the SELECT query on the<br />

database for the record associated with the String entered in txtLast. This represents<br />

the last name of the person whose record the user wishes <strong>to</strong> retrieve. Line 81 invokes<br />

method Clear of class DataSet <strong>to</strong> empty the DataSet of any prior data. Lines 85–87<br />

modify the text of the SQL query <strong>to</strong> perform the appropriate SELECT operation. The Ole-<br />

DbDataAdapter method Fill then executes this statement (line 91). Notice how a different<br />

overload of method Fill from the previous example has been used in this situation.<br />

Only the DataSet <strong>to</strong> be filled is passed as an argument. Finally, the TextBoxes are<br />

updated with a call <strong>to</strong> method Display (line 94).<br />

Methods cmdAdd_Click (lines 122–173) and cmdUpdate_Click (lines 176–<br />

232) perform INSERT and UPDATE operations, respectively. Each method uses members<br />

of class OleDbCommand <strong>to</strong> perform operations on a database. The instance properties<br />

InsertCommand and UpdateCommand of class OleDbDataAdapter are instances<br />

of class OleDbCommand.<br />

1 ' Fig. 19.29: AddressBook.vb<br />

2 ' Using SQL statements <strong>to</strong> manipulate a database.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Public Class FrmAddressBook<br />

7 Inherits Form<br />

8<br />

9 ' <strong>to</strong>p set of command but<strong>to</strong>ns<br />

10 Friend WithEvents cmdFind As But<strong>to</strong>n<br />

11 Friend WithEvents cmdAdd As But<strong>to</strong>n<br />

12 Friend WithEvents cmdUpdate As But<strong>to</strong>n<br />

13 Friend WithEvents cmdClear As But<strong>to</strong>n<br />

14 Friend WithEvents cmdHelp As But<strong>to</strong>n<br />

15<br />

16 ' textbox identifier labels<br />

17 Friend WithEvents lblId As Label<br />

Fig. 19.29 Database modification demonstration (part 1 of 9).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!