SqlDataAdapter
- It is a part of the ADO.NET Data Provider.
- SqlDataAdapter is used to retrieve data
from a data source and store the results in a DataSet.
- Dataset represents a collection of
data(tables) retrieved from the Data Source.
Diagram:
Steps:
- Create a query string.
- Create a connection object and open it.
- Create a SqlDataAdapter object
accompanying the query string and connection object.
- Create a DataSet object.
- Use the Fill method of SqlDataAdapter
object to fill the DataSet with the result of query string.
- Close the connection object
Example:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con;
SqlDataAdapter adapter ;
DataSet ds = new DataSet();
try {
//create
connection object
con = new SqlConnection("connetionString");
//create
query string(SELECT QUERY)
String query="SELECT * FROM SOMETABLE WHERE...";
con.Open();
//Adapter
bind to query and connection object
adapter = new SqlDataAdapter(query, con);
//fill the
dataset
adapter.Fill(ds);
}
catch (Exception ex)
{
con.Close();
}
}
Result:
Your DataSet contains the result table of SELECT query. You can play around the
table and its data.
Happy Learning...