By reading this article, you will learn key points in ADO.NET, as given below.
- What is Data Adapter?
- Primary Data Adapters for the databases.
- Data Adapter properties.
- Methods used by Data Adapter.
- How to create Data Adapter with an example?
Let’s start our demo with a Data Adapter introduction.
Actually, we use Data Adapter object to establish the connection to the data source and manage the movement of date to and from the database.
What is Data Adapter?
A data adapter aobject serves as a bridge between a data set object and Data Source such as a database to retrieve and save the data.
Data adapter contains a set of database commands and a database connection, which we use to fill a dataset object and update the Data Source.
Primary data adapters for databases?
.NET makes two primary data adapters available for use with the databases. Other data adapters can also be integrated with Visual Studio .NET.
Primary Data Adapters are mentioned below.
- OleDbData Adapter, which is suitable for use with certain OLE DB providers.
- SQLDataAdapaters, which is specific to a Microsoft SQL server. This is faster than the OleDBDataAdapter. because it works directly with SQL servers and does not go through an OLE Db Layer.
Data adapter properties
We use data adapter objects to act on records from a Data Source. We can also specify, which action we want to perform by using one of following four data adapter properties, which executes a SQL statement.
The properties are given below.
- Select command retrieves rows from Data Source
- Insert command writes inserted rows from data set into Data Source
- Update command writes modified rows from data set into Data Source.
- Delete command deletes rows from Data Source.
Methods used by a data adapter
Actually, we use data adapters to fill or to make changes in a data set table to a data store. These methods comprises of following.
- Fill
Use this method of a SQL data adapter to add or refresh row from a Data Source and place them in a Data Set table. The fill method uses Select statement, which is specified in the Select command property
- Update
Use this method of data adapter object to transmit the changes to a dataset table to the corresponding Data Source. This method calls the corresponding insert, delete or update command for each specified row in a data table in a data set.
- Close
Use this method for the connection to a database.
- Creating Data Adapter with Example
The examples given below use a SQLDataAdapter object to define a query in the database.
Code
- using System;
- using System.Data;
- using System.Data.SqlClient;
- namespace DataAdapter {
- class Program {
- static void Main(string[] args) {
- string connectionString = @ "data source=localhost;initial catalog=northwind;integrated security = SSPI";
- string commandString = @ "SELECT * from customers";
- SqlDataAdapter da = new SqlDataAdapter(commandString, connectionString);
- DataSet ds = new DataSet();
- DataAdapter.Fill(ds);
- DataTable dt = ds.Tables[0];
- int rows = dt.Rows.Count;
- }
- }
- }
Hence, this is the way of how can we create Data Adapter object, using ADO.NET. Thanks for reading this article. Sharing is caring.