ADO.NET:ADODataReader类

2016-01-29 18:24 5 1 收藏

ADO.NET:ADODataReader类,ADO.NET:ADODataReader类

【 tulaoshi.com - ASP 】

  原码下载地址:
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:ADONETdemoAuthors.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() + " c

来源:http://www.tulaoshi.com/n/20160129/1505661.html

延伸阅读
导 读:在ADO中我们最常使用的对象就Recordset了,而在ADO.NET中又增加了一个对象DataSet。本文简要的对比了DateSet和Recordset的异同,这对ADO.NET的初学者非常有帮助! 翻译整理:.net技术网(www.51dotnet.com)郜飞 原文出处:http://www.database-applications.net/articles/dotnet4.html Recordset是一个连接或断开的(通过使用游标)...
当转为使用ADO.NET时,您将需要了解如何应对以前知道用ADO处理而现在必须用ADO.NET解决的场景。就像使用Visual Basic、C++和ASP开发的N层解决方案经常要依赖ADO来满足数据访问需要一样,Windows?窗体、Web窗体和Web服务也要依赖ADO.NET。我曾经从使用传统ADO开发的角度讨论了如何使用ADO.NET来处理一些数据访问的场景。其中的一些主题包括将行...
标签: Web开发
要想充分发挥ADO.NET的优势,不仅需要全面、深入理解ADO.NET编程模型,及时总结经验、技巧也十分重要。ADO已经有多年的实践经验,ADO.NET以此为基础,提供了更加丰富、强大的工具;尽管如此,ADO.NET的设计目标毕竟不是提供一个即插即用的工具,它不会把所有的编程工作简化到仅靠鼠标点击就可以完成的程度。  ADO.NET包含了一大堆代表...
    ADO.NET提供了Connection来连接数据库,同时也提供了Command对象来查询数据库。同Connection对象一样,Command也有两种:OleDbCommand和SqlCommand.其区别同Connection对象。 要操纵数据库,必须先使用Connection来连接到数据库,再创建一个Command来查询。有几种创建方式,例:    SqlCommand cmd;  ...
假设有一下一个实体类。 using System; using System.Xml; using System.Xml.Serialization; namespace TestPerson { public class Person { public string FullName; [NonSerialized()] public string Password; public Male sex; } public enum Male { M, F } } 先决定用xml 序列化把对象的状态dump到一个xml文件。 代...

经验教程

55

收藏

41

精华推荐

ADO.NET 2.0 - 如何建立一个 DataView

ADO.NET 2.0 - 如何建立一个 DataView

梦里相约3

一个经典的ADO.NET入门例子

一个经典的ADO.NET入门例子

扛起钞票去砸你

SQL Server 2005上的CLR和ADO.NET 2.0

SQL Server 2005上的CLR和ADO.NET 2.0

你们太眼森啊

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部