How To Create The Data Adapter Object Using ADO.NET

ADO.NET

ADO.NET

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.
Data Adapter introduction

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.
  • FillUse 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
  • UpdateUse 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.
  • CloseUse 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.

About the Author:
By nature I have a very curious and inquisitive mind. I like to do well into matters deeply and solve problems by using a lateral thinking approach coupled with strong analytical and evaluative skills, with the ability to articulate risk issues or concerns objectively. Besides these, I have an 5 years of exp. in IT Industry, Specially Software development and my area of interest is in SharePoint13,16,Office 365,MVC,C#,JavaScript,SQL,SSRS,Project Server etc.”
Achievements: C#MVP, Member of the Month (January)
Personal Blog: HTTP://PREMKT.BLOGSPOT.MY/
Linkedin: HTTPS://WWW.LINKEDIN.COM/IN/RATHROLLAPREMKUMAR/

References:
Kumar, P. How To Create The Data Adapter Object Using ADO.NET by #RathrolaPremKumar. [online] Available at: http://premkt.blogspot.ie/2017/03/how-to-create-data-adapter-object-using.html [Accessed 5 Apr. 2017].

Share this on...

Rate this Post:

Share: