How to DataAdapter in Sql Server

SqlDataAdapter serves as the intermediary between the Dataset and the Data Source by utilizing the SqlConnection Object. The SqlConnection Object solely handles the communication aspect and does not possess any knowledge about the retrieved data. Similarly, a Dataset lacks information about the Data Source from which the data originates. Thus, the SqlDataAdapter bridges the gap between these two objects, managing the seamless communication between them.

SqlDataAdapter

DataAdapter plays a crucial role in retrieving data from a data source and populating tables within a Dataset. Additionally, it facilitates the synchronization of changes made to the Dataset back to the original data source. To populate data in a Dataset, the Fill method of the SqlDataAdapter is employed. The provided C# source code demonstrates a simple program that utilizes the SqlDataAdapter to retrieve data from the Data Source, using the SqlConnection Object, and subsequently populating the retrieved data within a Dataset.

Full Source C#
using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = null; SqlConnection connection ; SqlDataAdapter adapter ; DataSet ds = new DataSet(); int i = 0; connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"; connection = new SqlConnection(connetionString); try { connection.Open(); adapter = new SqlDataAdapter("Your SQL Statement Here", connection); adapter.Fill(ds); connection.Close(); for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { MessageBox.Show (ds.Tables[0].Rows[i].ItemArray[1].ToString()); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }