C# DataAdapter.Fill

The DataAdapter, within a Data Provider, is an object that encapsulates complex functionality. Its primary purpose is to establish a connection between a DataSet and a data source, enabling the retrieval and storage of data.

DataAdapter.Fill()

The Fill operation performed by the DataAdapter is a crucial aspect of its functionality. This operation involves retrieving rows from the data source and adding them to the corresponding DataTable objects within the DataSet. If the DataTable objects do not exist, the DataAdapter creates them.t.

DataAdapter.Fill(DataSet);
DataAdapter.Fill(DataTable);

The Fill method is responsible for retrieving rows from the data source, and it utilizes the SELECT statement specified by the associated SelectCommand property to perform this task. The DataAdapter adds or refreshes rows in the DataSet to match the data present in the data source. The matching is done using the DataSet name and creates a DataTable named "Table" if it doesn't already exist.

Full Source C#
using System; using System.IO; using System.Data ; using System.Data.SqlClient ; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlConnection cnn; string connectionString = null; SqlDataAdapter sqlAdp = default(SqlDataAdapter); DataSet ds = new DataSet(); DataSet dt = new DataSet(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int i = 0; connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=userid; Password=password" cnn = new SqlConnection(connectionString); cnn.Open(); sqlAdp = new SqlDataAdapter("select * from users", cnn); cnn.Close(); //connection close here , that is disconnected from data source sqlAdp.Fill(ds); sqlAdp.Fill(dt); //fetching data from dataset in disconnected mode for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { MessageBox.Show (ds.Tables[0].Rows[i].ItemArray[0].ToString()); } //fetching data from datatable in disconnected mode for (i = 0; i <= dt.Tables[0].Rows.Count - 1; i++) { MessageBox.Show(dt.Tables[0].Rows[i].ItemArray[0].ToString()); } } } }

Conclusion

Using the DataAdapter's Fill method, developers can seamlessly populate DataTable objects within a DataSet with data retrieved from the data source. This process ensures that the DataSet remains synchronized with the data source, facilitating efficient data manipulation and storage within the application.