程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ADO.NET:ADODataReader類

ADO.NET:ADODataReader類

編輯:.NET實例教程

原碼下載地址:
http://www.codeproject.com/dotnet/ADONET_datareader/ADONET_datareader.zip

Introduction
ADO.NET is the .NET enhanced version of ADO that we all know and love. ADO.Net aims to address some of the deficiencies of traditional ADO such as lack of type safety, lack of an object oriented model, and inefficiencIEs in returning rows of data.

This first article will demonstrate the most common task when Accessing a database: querying for data, and traversing that data from start to finish in order to display the contents (or subset thereof) of a table.

The ADODataReader class
ADO.Net replaces the concept of data rows with the DataSet object. This essentially provides us with full Access to a given database, including all rows, tables and relationships in an object orIEnted and type-safe manner. It is, however, total overkill for the simple query and traversals that are most often performed on databases.

For this simple case .Net provides us with the ADODataReader class that is essentially a type safe read only, forward only rowset. All we need to do is open a connection to a database, send an SQL command, then traverse through the resultant ADODataReader using the Read command and process the results.

The easIEst way to illustrate this is to show you some C# code. This code opens an Access database, reads all the information from a table, then populates a List VIEw control with the data inside.

A few notes on the code:

StatusText is a RichTextBox control declared as System.WinForms.RichTextBox StatusText;
StatusText = new System.WinForms.RichTextBox ();
listView is a list view control declared as System.WinForms.ListView listVIEw;
listView = new System.WinForms.ListVIEw ();
The list vIEw has been placed in report mode with grid lines using

listView.View = System.WinForms.VIEw.Report;
listVIEw.GridLines = true;


The Code
ADOConnection adoConnection = new ADOConnection();

// TODO: Change the location of this file
// The '@' means that the string will be treated as-is, and the
// '\'s will not be interpreted as the escape character.
// This saves typing "D:\\ADONETdemo..."
String strDatabaseFile = @"D:\ADONETdemo\Authors.mdb";

try
{
// Open a connection to the database
adoConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strDatabaseFile + ";" +
"Persist Security Info=False;";
adoConnection.Open();

// Create an SQL command, set its connection and its command text
ADOCommand command = new ADOCommand();
command.ActiveConnection = adoConnection;
command.CommandText = "SELECT * FROM Author";

// Execute the command, and return the rows in the data reader object
ADODataReader dataReader;
command.Execute(out dataReader);

// Get the number of fIElds (columns) in the table
int nFields = dataReader.FIEldCount;

// Setup the columns in the listview using the fIElds in the table
listVIEw.Clear();
for (int i = 0; i{
listVIEw.InsertColumn(i, dataReader.GetName(i), 100, HorizontalAlignment.Left);
}

// Fill the rows in the listvIEw using the data in the rows
int nRow = 0;
while (dataReader.Read())
{
// Create an array of subitems for quick insertion
// The subitems will be all fIElds in the row except for
// the first fIEld
String [] subitems = new String[nFIElds-1];
for (int i = 1; i{
subitems[i-1] = dataReader.GetValue(i).ToString();
}

// Insert a new item into the listvIEw, and add the subitems
// at the same time. The item will be the first fIEld in the
// row
listVIEw.InsertItem(nRow, dataReader.GetValue(0).ToString(),
-1, subitems);
// next row.
nRow++;
}
dataReader.Close();

// Set the status text
StatusText.Text = nFIElds.ToString() + " columns, " +
nRow.ToString() + " rows read";
}
catch
{
// If an error occured alert the user
StatusText.Text = "Error occurred";
}
finally
{
// Close the connection if necessary
if (adoConnection.State == DBObjectState.Open)
adoConnection.Close();
}

That's all there is to it. We have closed the database connection but since we are using managed code there is no need (or way) to delete the objects and memory we allocated.

About Chris Maunder
Chris is the founder and site administrator for CodeProject.com.He's been programming in C/C++ for 10 years and Visual C++/MFC for 4 years. His background includes pure and applIEd mathematics, engineering and physics, and he is currently based in Canberra, Australia.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved