Tải bản đầy đủ (.pdf) (69 trang)

Beginning Visual Basic .NET Database Programming phần 6 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (503.88 KB, 69 trang )

Data Binding
45
Dim strSQL As String = "SELECT * FROM Products"
Then, we open a new SQLConnection using the connection string.
Dim sqlConn As New SqlClient.SqlConnection(strConnection)
sqlConn.Open()
The myCommand data command is created by passing it the statement and connection strings. We can
then use myCommand to cause the DataReader to execute the SQL statement that we want – in this
case, to select all records from the Products table.
Dim myCommand As New SqlClient.SqlCommand(strSQL, sqlConn)
myCommand.CommandType = CommandType.Text
Dim myReader As SqlClient.SqlDataReader = myCommand.ExecuteReader()
However, as we learned previously, the DataReader retrieves the records one at a time in a forward-
only and read-only format.
The last part of code retrieves each record in the DataReader one at a time and writes the Product Id
and Product Name to the Output window using the Console.WriteLine method:
Do While myReader.Read
Console.WriteLine("Product Id: " & myReader.GetInt32(0) & _
vbTab & "Product Name: " & myReader.GetString(1))
Loop
Notice the use of the GetInt32 and GetString methods of the DataReader object. These methods
can be used because we know the data types of the underlying data fields. By explicitly using the typed
accessor methods in this way, we reduce the amount of type conversion that is required when retrieving
the data.
Last of all, we have the line that closes the DataReader.
myReader.Close()
It is very important to note that a DataReader is open until you close the connection with such a line
of code (or the object gets destroyed and thus the connection is closed at some point during garbage
collection).
You can modify this simple example for your own purposes, but this should give you an idea of how to
a DataReader can be used to rapidly retrieve forward-only, read-only data. The most important idea


to take away from this section is that you should use a DataReader whenever possible, and especially
when an in-memory copy of data is not required.
Chapter 8
46
Summary
In this chapter we've covered some crucial data binding concepts as we further developed our Product
Management System. We bound our search results to the DataGrid and added the functionality to
allow the user to open a specific record in the results list on the Add/View/Edit Products or Suppliers
screens. We specifically learned about:
❑ Complex data binding to bind controls to more than one element in a DataSet
❑ Simple data binding to bind to the property of a control to a single element in a DataSet
❑ Creating the Base Data Form for our Product Management System
❑ Creating the Add/View/Edit Products and Suppliers Screens that inherit functionality from
the Base Data Form
❑ Customizing the Add/View/Edit Screens for their specific needs
❑ Validating user input using the ErrorProvider Control
❑ Filtering and sorting data using DataViews
❑ Returning records using the DataReader
You should have a pretty good understanding of how to implement complex and simple data binding
and should also have working Search Screens. In the next chapter, we will continue with the
development of our application and begin implementing functionality to allow the user to update data
from the Add/View/Edit Products and Suppliers screens.
Exercises
3. What is the difference between complex data binding and simple data binding? Where does
property binding fit in?
4. Briefly describe the ErrorProvider control and what it can allow you to accomplish.
5. What is the purpose of a DataView?
6. Briefly describe the DataReader and when it can be used. Can you bind a DataReader to
controls on a form such as a DataGrid? When should you use a DataReader versus a
DataSet?

Answers are available at />Data Binding
47
Chapter 8
48

×