|

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 and present
a model that provides type safety, OOP, and efficiency.
This 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 DataReader 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 DataReader class
(OleDbDataReader, OdbcDataReader and
SqlDataReader) that is essentially a type safe read only, forward
only rowset. The differences between the various flavours of
DataReaders is in which data access library they use.
SqlDataReader works best with SQL Server, while the others work
best with ODBC and OleDB data srouces.
All we need to do is open a connection to a database, send an SQL command,
then traverse through the resultant DataReader using the
Read command and process the results.
The easiest way to illustrate this is to show you some code. This snippet
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 and fileName are
RichTextBox controls declared as private System.Windows.Forms.RichTextBox fileName;
private System.Windows.Forms.RichTextBox StatusText;
listView is a list view control declared as System.WinForms.ListView listView;
listView = new System.Windows.Forms.ListView ();
The list view has been placed in details mode using listView.View = System.Windows.Forms.View.Details;
The CodeOleDbConnection Connection = new OleDbConnection ();
try
{
Connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + fileName.Text + ";" +
"Persist Security Info=False;";
Connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = Connection;
command.CommandText = "SELECT * FROM Authors";
OleDbDataReader dataReader;
dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
int nFields = dataReader.FieldCount;
listView.Clear();
for (int i = 0; i < nFields; i++)
{
listView.Columns.Add(dataReader.GetName(i), 100,
HorizontalAlignment.Left);
}
int nRow = 0;
while (dataReader.Read())
{
String [] subitems = new String[nFields];
for (int i = 0; i < nFields; i++)
{
subitems[i] = dataReader[i].ToString();
}
ListViewItem item = new ListViewItem(subitems, -1);
listView.Items.Add(item);
++nRow;
}
dataReader.Close();
StatusText.Text = nFields.ToString() + " columns, " +
nRow.ToString() + " rows read";
}
catch (Exception ex)
{
StatusText.Text = "Error: " + ex.Message;
}
finally
{
if (Connection.State == System.Data.ConnectionState.Open)
Connection.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.
History
23 Aug 2003 - Updated to .NET 1.0/1.1. Previous version was for .NET beta
1.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh) | FirstPrevNext |
|
 |
|
|
Hello, I want to know how to use OleDbDataReader.Item() with a field name as a parameter to it. It doesnt seem to work. Is the only way to access by the field index?
Any help be appreciated!
Tan Q!
Charu.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hi i have a code like this... it works fine without any conditions(parameters). But when i give the parameters it returns null. but if I copy the query (at runtime) and run it, it returns rows in oracle...pls help whats wrong with my code
ConnStr = "Provider=oraOLEDB.oracle.1;User ID=ss;Password=ss;Data Source=ss"; cn->ConnectionString = ConnStr; cn->Open(); strSql1= String::Concat(S"Select * from TEST WHERE TEST.CREATEDdate BETWEEN ? AND ?"); cmd->CommandType=CommandType::Text; cmd->Connection=cn; cmd->CommandText = strSql1; OleDbParameter *p1 = new OleDbParameter(); OleDbParameter *p2 = new OleDbParameter(); cmd->Parameters->Add(p1); cmd->Parameters->Add(p2);
p1->Value = S"27-Apr-2006"; p2->Value = S"28-Apr-2006"; OleDbDataReader *Rdr = cmd->ExecuteReader(); Rdr->get_RecordsAffected(); while(Rdr->Read()) { textBox1->Text = Rdr->GetValue(1)->ToString(); } Rdr->Close();
Please help its annoying...
Salai
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
How do I bind a radiobutton in ado.net? Do I use this code: radiobutton1.databindings.add("Selected", dsdataset, "sample.field")?
If so, how does it determine which value? ie. If I have a field postagedelivery that stores the following (FedEx,UPS, USPS). How do I do this in vb.net, help?!!!
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
can send a oledbdatareader as return parameter for function ex.
public function ssss() as oledbdatareader ... ... dim d as oledbdatareader = ... return d end function
and how can i use it in the reciever method (fun or sub) ????
plzzz tell me quick
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
u r doing "select * from any_table_name" but i want to know how many tables existes in the database.. what r there names.... how many columns they have and what r there names...
Nothing ...........................................................
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
DataReader it's ok, when you have to read 25 or 30 registers, but for more it's no practical quite slow...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
You know something that is faster? I always thought DateReader was the faster way to get rows using ADO.NET...
-- Quentin Pouplard (Tene/Graff Project) http://graff.alrj.org
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
I'm attempting to connect a vb .net program to an access database on a network drive but am having trouble. If the database is on the local drive, it works great. When I move the database to the network, I get an error message "No value given to one or more required parameters" . I have no clue as to what this means. All I have done is moved the database and changed the connection string to the appropriate location. Any help will be greatly appreciated.
CADDSMAN
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Whats your connection string ?...also check if you have rights to access the folder in the network where you've placed your access db.
Regards, Kannan
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for the info. The connection string is: Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=W:\Shared Documents\Databases\CPJ\Archived Plans.mdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False
I was told that I have full access rights.
Thanks, Yale
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
You shoud look at your .NET security configuration. Accessing network files from .NET application is prohibited by default. Admin should give permission to certain application to access certain network places. Sorry, I don't remember all the details, but the direction is right.
Good luck
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi everybody ok we can return multiple recordsets from a stored procedure in classical asp we used to navigate through these multiple recordsets using the "nextRecordSet()"
now with the .net ! the same stored procedure that returns multiple recordsets is used. we are reading the result using an oledbdatareader we start with the first "recordset" using the read method so how do we shift to the next recordset to start reading it? please help
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
Do not worry, there is no ADODataReader in the .NET framework. Probably there was one in the beta 1, but was finally replaced with OLE DB.
Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Chris,
I have some code in which I am attempting to use two different OleDbCommand and OleDbDataReader objects against a single OleDbConnection. This would be like useing multiple ADODB.Recordset objects against a single ADODB.Connection object, something I am very used to doing. However, I get an error on the .ExecuteReader method saying:
"There is already an open DataReader associated with this Connection which must be closed first."
Can you not have multiple Command/DataReader objects on a single connection?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
From my experience you can't have multiple datareaders open at the same time on the same connection. I could be wrong since I was playing with this sort of stuff on an early beta.
cheers, Chris Maunder
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Hi, This is what in .NET Framework SDK document: "While the OleDbDataReader is in use, the associated OleDbConnection is busy serving the OleDbDataReader, and no other operations can be performed on the OleDbConnection other than closing it. This is the case until the Close method of the OleDbDataReader is called. For example, you cannot retrieve output parameters until after you call Close."
So I'm sure that you can use one connection with multiple OleDbDataReader, but you have to close the opening OleDbDataReader before open a new one.
Hope it helps!
lizhill --- I want to learn ---
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Chris,
I like it. Is it appropriate (or possible) to return, say a SqlDataReader object from a Web Method?
Thanks, Ron
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
If you are scanning the whole result set I guess it will take forever on large data amounts. The MFC CListCtrl has SetRowCount method and can fetch data on demand. Is something similar also possible with WinForms and c# ?
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|