Question - What is a DataAdapter in ADO.NET?
Answer -
- A DataAdapter is used to access data from a data source by functioning as a bridge between DataSet and a data source. DataAdapter class includes an SQL command set and a database connection. It is helpful to fill the DataSet and resolve changes to the data source.
- The DataAdapter will make use of the Connection object that belongs to the .NET Framework data provider for connecting with a data source. Along with that, it will also use Command objects to retrieve data from the data source as well as to resolve changes to the data source.
- DataAdapter properties that permit the user to control the database are the Select command, Update command, Insert command, and Delete command.
- Example code for the usage of DataAdapter:
using System;
using System.Data.SqlClient;
using System.Data;
namespace DataAdapterExample
{
public partial class DataAdapterDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("data source=.; database=items; integrated security=SSPI"))
{
SqlDataAdapter da = new SqlDataAdapter("Select * from items", conn);
DataSet s = new DataSet();
da.Fill(s);
GridView1.DataSource = s;
GridView1.DataBind();
}
}
}
}
Here, DataAdapter will receive the data from the items table and fill the DataSet, which will be later used to display the information retrieved from the items database.