Home arrow ADO.NET arrow Page 2 - Data Access in .NET using C#: Part 1

Data Access in .NET using C#: Part 1

ADO.NET and C# can be used together to work with an Access database to produce a simple application. In this article Sachin shows us how it's done.

Author Info:
By: Sachin Korgaonkar
Rating: 4 stars4 stars4 stars4 stars4 stars / 164
November 15, 2002
  1. · Data Access in .NET using C#: Part 1
  2. · Data Access in .NET
  3. · The DataReader Object
  4. · Conclucion

print this article

Data Access in .NET using C#: Part 1 - Data Access in .NET
(Page 2 of 4 )

Data access is an important part of any web application. Relational database management systems (RDBMS's) are the most common items used in a web application. ADO.NET is the primary and most important -- technology for working with RDBMS's in .NET.

ADO.NET is not the next version of ADO (ActiveX Data Objects). ADO provided many features however. In fact, previous data access models were designed around two-tier development. ADO ran on top of COM. ADO.NET supports the Microsoft.NET framework and its many features. It was built from the ground up, and this is the reason why ADO.NET handles XML very well. XML technology guarantees interoperability because it is an industry standard format.

In ADO.NET, data is represented and marshaled as XML automatically. Data can be serialized directly from XML for faster, more portable transfer across a network. Disconnected databases are a new feature in ADO.NET. They are not completely new, because ADO 2.0 introduced disconnected RecordSets for scalability, however if too many sessions were opened at the same time, then it effected system performance. Using a disconnected database, we can open a connection for short period of time. A DataSet can hold an entire database or part of a database in memory.

Because DataSets are disconnected, they must use a DataAdapter object to keep track of database changes. The ADO.NET object model classes are defined in two groups: content component and managed-provider component. The content component is the most important component because it includes the DataSet and DataRelation classes. The managed-provider components can be used to directly manipulate data. The data can be in any format, such as an XML file, Excel or ASCII text files.

The System.Data.OleDbClient namespace includes OLE DB support and the System.Data.SqlClient namespace supports Microsoft SQL Server 7 and above.

The Data Access Object Model of ADO.NET

A Practical Example
Let's take one short example for ADO.NET. We will use an MS-Access Database. Start VisualStudio.NET IDE and create new C# Windows Application Project. Add one list box and button onto your form, as shown below:

Application Screen

Call the list box "lb" and call the button "btn". Set the buttons text property to "Retrieve Data". Right click on the form and choose view code from the menu. Add these namespaces to the code:

using System.Data;
using System.Data.OleDb;

The System.Data.OleDb namespace contains the components necessary to implement the OLE DB data provider. Now, double click on the button control and add this code into the buttons click event:

private void button1_Click(object sender, System.EventArgs e)


OleDbConnection Myconnection = null;
OleDbDataReader dbReader = null;

Myconnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; Password=; Data Source=C:\familyTree.mdb");

OleDbCommand cmd = Myconnection.CreateCommand();
cmd.CommandText = "SELECT * FROM familyTree";
dbReader = cmd.ExecuteReader();

string Email;

Email = (string)dbReader.GetValue(5);



To make a connection to the database, we should use a connection object from the ADO.NET Classes. Before we do anything with the database, we must have an active, working connection to it. Connection objects have different parameters, dependant upon the underlying database. For example, if we use an MS-Access database, then this code will establish a session with the database:

OleDbConnection Myconnection = null;

Myconnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; Password=; Data Source=C:\familyTree.mdb");

An OleDbConnection object represents a unique connection to a data source. When you create an instance of OleDbConnection, all properties are set to their initial values. The OleDbconnection object has one string parameter with four attributes for the connection. The are as follows:
  • Provider: This is dependant upon the underlying database. In this example we use the Microsoft Jet database engine to connect to a MS-Access database.

    We can add new vender-specific data providers. OLEDB and SQL data providers are the standard data providers that come with the .NET framework.
  • User Id and Password: The username and password to connect to the database.
  • Data Source: This is the physical location of the database file.

After the connection object has been created, it can be opened with the Open() method. This method is available in the OleDBConnection object.
blog comments powered by Disqus

- Datasets in Microsoft.Net
- Latest Developments in the .Net World
- Introduction to .NET
- Automatic Generation of Single Table SQL Sta...
- Data Access in .NET using C#: Part 1
- All You Need To Know About ADO.NET: Part 2/2
- All You Need To Know About ADO.NET: Part 1/2
- Easing Transition From ASP and ADO to ASP.NE...
- A Practical Comparison of ADO and ADO.NET

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials