tailieunhanh - Beginning Visual Basic .NET Database Programming phần 6

Các lệnh myCommand dữ liệu được tạo ra bằng cách báo cáo và chuỗi kết nối. Sau đó chúng tôi có thể sử dụng myCommand gây ra DataReader để thực hiện các câu lệnh SQL mà chúng ta muốn - trong trường hợp này, để chọn tất cả hồ sơ từ bảng Sản phẩm | Data Binding Dim strSQL As String SELECT FROM Products Then we open a new SQLConnection using the connection string. Dim sqlConn As New strConnection 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 strSQL sqlConn Dim myReader As 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 method Do While Product Id 0 _ vbTab Product Name 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. 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. 45 Chapter 8 Summary In this chapter we ve .