原码下载地址:
快乐开发技术文档http://www.codeproject.com/dotnet/ADONET_datareader/ADONET_datareader.zip
Introduction
一起娱乐网01716.com
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.
一起娱乐网01716.com
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.
一起娱乐网01716.comThe 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;
javascript技巧尽在快乐开发
The Code
ADOConnection adoConnection = new ADOConnection();
// TODO: Change the location of this file
Powered By Achely's Blog// The '@' means that the string will be treated as-is, and the
javascript技巧尽在快乐开发// '\'s will not be interpreted as the escape character.
.net开发技术文章// This saves typing "D:\\ADONETdemo..."
String strDatabaseFile = @"D:\ADONETdemo\Authors.mdb";
try
一起娱乐网01716.com{
快乐开发技术文档// Open a connection to the database
adoConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
快乐开发技术文档"Data Source=" + strDatabaseFile + ";" +
zhangyongjun.com开发网"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
一起娱乐网01716.comADODataReader dataReader;
command.Execute(out dataReader);
// Get the number of fields (columns) in the table
zhangyongjun.com开发网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())
Powered By Achely's Blog
{
// Create an array of subitems for quick insertion
// The subitems will be all fields in the row except for
Powered By Achely's Blog
// the first field
String [] subitems = new String[nFields-1];
for (int i = 1; i<nFields; i++)
{
zhangyongjun.com开发网
subitems[i-1] = dataReader.GetValue(i).ToString();
devjoy.cn技术文档
}
devjoy.cn技术文档一起娱乐网01716.com
// 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
Powered By Achely's Blog
listView.InsertItem(nRow, dataReader.GetValue(0).ToString(),
-1, subitems);
快乐开发技术文档
// next row.
nRow++;
javascript技巧尽在快乐开发
}
dataReader.Close();
zhangyongjun.com开发网
// Set the status text
StatusText.Text = nFields.ToString() + " columns, " +
nRow.ToString() + " rows read";
}
catch
devjoy.cn技术文档
{
// If an error occured alert the user
StatusText.Text = "Error occurred";
}
一起娱乐网01716.comfinally
javascript技巧尽在快乐开发{
// 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.