频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> Asp.Net教程 -> ADO.NET:ADODataReader类

ADO.NET:ADODataReader类

作者:未知  来源:转载  发布时间:2005-9-17 0:21:46  发布人:acx

减小字体 增大字体

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

将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· 93%涉华域名被判转移 注..
· 显示用户是否在线的方法..
· 用ASP开发一个在线考试程..
· 使用Web标准建站第4天:..
· 精通数据库系列之入门-基..
· 得心应手 X-Space下的二..
· 用Socket发送电子邮件(..
· 中文PHP.INI(2)
· ASP实现多语言支持
· 将ASP代码移植为VB COM组..
· 影响网站排名的一些问题..
· 典型的DIV+CSS三行二列居..
· 聊天室技术(六)-- 表..
· ASP.NET ViewState 初探..
· ADO.NET 2.0 Feature Ma..
· 如何避免重复定义数组
相关文章
· ado.net 如何读取 excel
· ADO.NET访问数据库的步骤
· ADO.NET 数据库实例教程
· 教学体会: ADO.NET的连接式..
· ADO.NET:使用ADO.NET连接文..
· 漫谈.Net PetShop和Duwamis..
· 漫谈.Net PetShop和Duwamis..
· 漫谈.Net PetShop和D..
· 在 ADO.NET 数据集中浏览多..
· 在 ADO.NET 数据集中..
· 在 ADO.NET 数据集中浏览多..
· 在 ADO.NET 数据集中..
· 在 ADO.NET 数据集中浏览多..
· 在 ADO.NET 数据集中浏览多..
· ADO.NET入门(6)
· ADO.NET入门(5)
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号