ADO.NET
ADO.NET
ADO.NET
This article introduces the ADO.Net object model. In addition, it explains how to create and manage
connections to a database.
Most applications need to handle data, whether it is in the form of a dataset, a text file, or a spreadsheet.
The majority of modern-day applications need to deal with various types of databases. Therefore, to
access this data the application needs to interact with various databases, such as Microsoft SQL Server,
Oracle, Microsoft Access, and so on.
ADO.NET is a part of the .NET framework architecture. It is a model used by .NET applications to
communicate with a database for retrieving, accessing, and updating data, as shown in the following
figure.
ADO.NET Object Model
In the ADO.NET object model, the data residing in a database is retrieved through a
data provider. The data provider is the set of components including the Connection,
Command, DataReader, and DataAdapter objects. An application can access data
either through a dataset or through a datareader object.
Data Provider
DataSet
Data Provider
Selecting an appropriate data provider for a client application depends on the type of
data source being accessed. There are four .Net data providers available.
1. SQL Server: It's used to work specifically with Microsoft SQL Server. It exists in a
namespace within the System.Data.SqlClient.
2. OLE DB: It's used to work with the OLEDB provider. The System.Data.dll assembly
implements the OLEDB .NET framework data provider in the System.Data.OleDb
namespace.
3. ODBC: To use this type of provider, you must use an ODBC driver. The
System.Data.ODBC.dll assembly implements the ODBC .NET framework data
provider. This assembly is not part of the Visual Studio .NET installation.
4. Oracle: The System.Data.OracleClient.dll assembly implements the Oracle .NET
framework data provider in the System.Data.OracleClient namespace. The Oracle
client software must be installed on the system before you can use the provider to
connect to an Oracle data source.
The connection string provides the information that defines the connection to the database.
Note: In this example, I'm using a sample database present in my datasource. Here you
need to create a sample database and the tblEmployee table in that database and
populate the tblEmployee table with some values.
Step 1. Create a Windows Form application and design the form as shown in the
following figure.
Step 2. Add the following two namespaces.
System.Data.SqlClient;
System.Data;
Step 3. On click event of the Button write the following code.
// Open Connection
cn.Open();
da.SelectCommand = cmd;
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
Note. We will see more about SqldataAdpter's methods and properties in my future
articles on ADO.NET.
Step 4. Execute the application and verify the output by clicking on the button control. If
everything goes fine then you will get the following output.