Tutorials to .com

Tutorials to .com » Dotnet » Database » Microsoft Access using ADO.NET data unlock (2)

Microsoft Access using ADO.NET data unlock (2)

Print View , by: iSee ,Total views: 8 ,Word Count: 1502 ,Date: Tue, 9 Jun 2009 Time: 6:24 AM

DataReader object to the use of moving forward only, read-only access to data format Very often, you just want to simply browse the data, and do not need the data back and forth between browsing, do not need to change the data (we call this "water data "). DataReader object in ADO.NET is specifically designed for this purpose. The following is a sample I prepared a section of code, can be read by the database to connect all the selected data:

'Visual Basic. NET code.
'Console application.
'Quote:
'System
'System.Data
'System.XML

Imports System.Data.OleDb 'for OleDb object.
Imports Microsoft.VisualBasic.ControlChars' for CrLf constant.

Module Module1

Sub Main ()

'Create and initialize the OleDbConnection, OleDbCommand
'And OleDbDataReader object.
Dim objConn As New _
OleDbConnection ( "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"User ID = Admin;" & _
"Data Source = C: \ Program Files \ Microsoft" & _
"Office \ Office10 \ Samples \ Northwind.mdb")

objConn.Open ()

'The implementation of the command and data reader attached to the
'The selected data.
Dim objCmd As New OleDbCommand ( "SELECT * FROM Products", _
objConn)
Dim objReader As OleDbDataReader = objCmd.ExecuteReader

'Read the data and the value listed.
Call ReadData (objReader)

End Sub

Public Sub ReadData (ByVal objReader As OleDbDataReader)

'Objective: To list the data given the data value read.
'Accepted: objReader - data reader.

Dim intField As Integer 'current field line.
Dim intColumn As Integer 'current column name.
Dim blnColumns As Boolean = False 'name has been listed
'List.

With objReader

'Read a line, until end of file.
Do While. Read = True

For intField = 0 To. FieldCount - 1

'First of all, the column names are listed.
If blnColumns = False Then

For intColumn = 0 To. FieldCount - 1

If intColumn =. FieldCount - 1 Then
Console.Write (. GetName (intColumn) & _
CrLf)
Else
Console.Write (. GetName (intColumn) & _
, "")
End If

Next intColumn

'Only listed the name of a column.
blnColumns = True

End If

'Listed in the current row in the value of each field.
If intField =. FieldCount - 1 Then
Console.Write (. Item (intField) & CrLf)
Else
Console.Write (. Item (intField) & ",")
End If

Next intField

Loop

End With

'Suspended in the console window so that users view the data.
Console.Write ( "Press any key to continue ...")
Console.Read ()

End Sub

End Module

The following is the code works:

As shown in the preceding code sample, Imports source (for example, Imports System.Data.OleDb) help to reduce the access of members of the target or object of the workload required. Similarly, the OleDbConnection, OleDbCommand and OleDbDataReader objects and initialization statement to represent the database connection, data records and records of the cursor. This code is the core of the real subroutine ReadData.
Do recycling OleDbDataReader object to read the Read method of the first line of data until the method returns False (that has no other data can be read).
OleDbDataReader object to return data FieldCount line attribute data field (column) number. If this is the first line of data, then call out the OleDbDataReader object attributes GetName to return the name out.
OleDbDataReader object properties Item properties FieldCount combined with the use of an index number to return to the data line, the value of each data field.
The use of DataAdapter, DataSet, DataTable, DataRow and DataColumn objects to deal with data sets
ADO.NET is the DataSet object is disconnected from the design of the data. Your data in a simulation of the entire database structure and data, including tables, rows, columns, fields, and even relations. Processed data, enabling data the original data and the data synchronization database. I prepared the following sample code section, you can disconnect one by one to read the data of all data.

'Visual Basic. NET code.
'Console application.
'Quote:
'System
'System.Data
'System.XML

Imports System.Data.OleDb 'for OleDb object.
Imports Microsoft.VisualBasic.ControlChars' for CrLf constant.

Module Module1

Sub Main ()

'Create and initialize OleDbDataAdapter and DataSet objects.
Dim objAdapter As New OleDbDataAdapter _
( "SELECT * FROM Products", _
"Provider = Microsoft.Jet.OLEDB.4.0;" & _
"User ID = Admin;" & _
"Data Source = C: \ Program Files \ Microsoft" & _
"Office \ Office10 \ Samples \ Northwind.mdb")
Dim objDataSet As New DataSet ()

'Will copy the selected data to the data sets.
objAdapter.Fill (objDataSet)

'Listed in the data value data.
Call ListData (objDataSet)

End Sub

Public Sub ListData (ByVal objDataSet As DataSet)

'Objective: To set out in a given data set of data values.
'Accepted: objDataSet - data sets.

Dim objTable As DataTable
Dim intRow, intColumn As Integer

'Data may contain multiple tables.
For Each objTable In objDataSet.Tables

With objTable

Console.Write ( "Table" & _
. TableName "data" & ":" & CrLf)

'First of all, the column names are listed.
For intColumn = 0 To. Columns.Count - 1

If intColumn =. Columns.Count - 1 Then
Console.Write (. Columns (intColumn). ColumnName _
& CrLf)
Else
Console.Write (. Columns (intColumn). ColumnName _
& "")
End If

Next intColumn

'According to the data line lists ...
For intRow = 0 To. Rows.Count - 1

'... And then listed according to field data in each row.
For intColumn = 0 To _
. Rows (intRow). ItemArray.Length - 1

If intColumn = _
. Rows (intRow). ItemArray.Length - 1 Then
Console.Write (. Rows (intRow). _
ItemArray (intColumn) _
& CrLf)
Else
Console.Write (. Rows (intRow). _
ItemArray (intColumn) & ",")
End If

Next intColumn

Next intRow

End With

Next objTable

'Suspended in the console window so that users view the data.
Console.Write ( "Press any key to continue ...")
Console.Read ()

End Sub

End Module

The following is the code works:

Similarly, Imports source (for example, Imports System.Data.OleDb) help to reduce the access of members of the target or object of the workload required. Similarly, the OleDbDataAdapter and DataSet objects and initialization statement, representing the database data sets and between the adapter and the data sets. FillData method OleDbAdapter object database will be copied to the data sets. This code is the core of ListData subroutine.
DataSet object by using the Tables property, the most outer circle of the For ... Each traverse data for each table and each table as a DataTable object to return.
DataTable object returns a Columns property DataColumnCollection, on behalf of all the columns in the table. Columns by combining the use of attributes and an index number (that is, combined with the Count property DataColumnCollection collection, such as shown in the code), will return to a DataColumn object. ColumnName property of the DataColumn object to return the name out.
Similarly, DataTable object's Rows property returns a DataRowCollection, on behalf of all rows in the table. Rows through a combination of attributes and an index number (that is, combined with the Count property DataRowCollection collection, such as shown in the code), will return a DataRow object. DataRow object returns an Object Properties ItemArray types of arrays, representing the data value for each line. ItemArray through a combination of attributes and an index number (that is, combined with the Count property DataColumnCollection collection, such as shown in the code), will return to a single data field values.
For more information on the ADO.NET For more information, please refer to the following:

ADO.NET (English, link list)
ADO.NET for the ADO Programmer (English, technical articles)
Accessing Data with ADO.NET (English, link list)
Views and Filters (in English, Technical Articles)
Data Relations and Relatives (in English, Technical Articles)
- Transfer from Microsoft in China


.Net Database Articles


Can't Find What You're Looking For?


Rating: 0.0

Comments

No comments posted.