ADO.NET:ADODataReader类

原码下载地址:
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<nFields; 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<nFields; 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.

时间: 2024-09-17 04:36:14

ADO.NET:ADODataReader类的相关文章

ado连接sql数据库-ADO的Recordset类的Open方法的用法

问题描述 ADO的Recordset类的Open方法的用法 在MFC里用ADO连接数据库 下面这一句是测试用的,是正确的: m_pAdoRecordset->Open("select UserName from OJUser where UserName = 'admin'",_variant_t((IDispatch *)m_pAdoConnect,true),adOpenDynamic,adLockOptimistic,adCmdText); 但是因为我要根据输入值来获取查询

使用C#语言操作ADO数据库

ado|数据|数据库 访问数据库是大多数应用程序的一部分,而且随着C#和ADO.NET的发布,这个过程已经变得相当的简单.本文将展示下面四个基本的数据库操作: 1.读数据.这包括诸如整数,字符串和日期等不同的数据类型.2.写数据.就象读数据一样我们会写这些通常的数据类型.这是通过SQL语句来实现的.3.更新或是修改数据.我们还是使用简单SQL语句.4.删除数据.使用SQL. 这些操作是对一个微软Access 2000数据库进行的,但是SQL或是其它ADO数据源可以通过简单的改变连接字符串来使用.

用C#对ADO.NET数据库完成简单操作

ado|数据|数据库 数据库访问是程序中应用最普遍的部分.随着C#和ADO.NET的引入,这种操作变得更简单.这篇文章将示范四种最基础的数据库操作.      ● 读取数据.其中包括多种数据类型:整型,字符串,日期型.      ● 写数据.如读数据一样,我们也需要写入多种类型的数据.这可以通过SQL语句来完成.      ● 更新或修改数据.我们将再次用到SQL语句.      ● 删除数据.用SQL实现.      上述的操作都要基于Microsoft Access 2000数据库,但是,我

在managed C++应用中使用ADO.NET

摘要: 文中就用ADO.NET数据对象访问数据库及从数据库回取数据的基本原理作出示例说明. 正文: 本文提供了一个由应用向导生成的基于标准Managed C++应用的实例,它用.NET的ADODataReader类从数据库中取回一个只读(read-only).只前移(forward-only)的数据流.就是因为在内存中一次仅一行,可用数据读取器(Data Reader)产生应用执行和化简系统套头(overhead).在主源文件增加如下代码能获取对具有数据库支持的.NET Framework类的访

用C#对ADO.NET数据库完成简单操作的方法_C#教程

数据库访问是程序中应用最普遍的部分.随着C#和ADO.NET的引入,这种操作变得更简单.这篇文章将示范四种最基础的数据库操作.  ● 读取数据.其中包括多种数据类型:整型,字符串,日期型.  ● 写数据.如读数据一样,我们也需要写入多种类型的数据.这可以通过SQL语句来完成.  ● 更新或修改数据.我们将再次用到SQL语句.  ● 删除数据.用SQL实现.  上述的操作都要基于Microsoft Access 2000数据库,但是,我们要对连接字符串进行简单的修改才能使用SQL或其他ADO数据.

从 ADO“经典”迁移到 ADO.NET

ado 本文摘自 Hitchhiker's Guide to Visual Studio and SQL Server 2005(7th Edition) William VaughnBeta V Corporation 适用于Microsoft ADO.NETMicrosoft SQL Server 2005(代号"Yukon")Microsoft Visual Studio 2005(代号"Whidbey") 摘要:Bill Vaughn 讨论了 Visual

为ADO 程序员设计的 ADO.NET (转)

ado|程序|程序员|设计 摘要:本文讨论如何以 ADO.NET 方式实现基本数据库操作,以及何时使用 ADO.NET 代替 ADO. 目录.NET 中的数据访问读取数据DataSet.DataTable 和 Recordset转换现有代码更新数据XML 扩展支持总结 自若干年前推出开放式数据库连接 (ODBC) 应用程序编程接口 (API) 以来,出现了各种各样的数据库访问技术,而 ADO.NET 是其中最新的一种.在这过程中,发生了许多有趣的事.例如,COM 闯入数据库领域,开始培植 OLE

为 ADO 程序员设计的 ADO.NET

ado|程序|程序员|设计 摘要:本文讨论如何以 ADO.NET 方式实现基本数据库操作,以及何时使用 ADO.NET 代替 ADO. 目录.NET 中的数据访问读取数据DataSet.DataTable 和 Recordset转换现有代码更新数据XML 扩展支持总结 自若干年前推出开放式数据库连接 (ODBC) 应用程序编程接口 (API) 以来,出现了各种各样的数据库访问技术,而 ADO.NET 是其中最新的一种.在这过程中,发生了许多有趣的事.例如,COM 闯入数据库领域,开始培植 OLE

为 ADO 程序员设计的 ADO.NET (1)

ado|程序|程序员|设计 为 ADO 程序员设计的 ADO.NET 摘要:本文讨论如何以 ADO.NET 方式实现基本数据库操作,以及何时使用 ADO.NET 代替 ADO. 目录 .NET 中的数据访问读取数据DataSet.DataTable 和 Recordset转换现有代码更新数据XML 扩展支持总结自若干年前推出开放式数据库连接 (ODBC) 应用程序编程接口 (API) 以来,出现了各种各样的数据库访问技术,而 ADO.NET 是其中最新的一种.在这过程中,发生了许多有趣的事.例如