Net-informations.com
SiteMap  | About    

C# DataAdapter.Fill

The DataAdapter is functionally the most complex object in a Data Provider. The DataAdapter serves as a bridge between a DataSet and a data source for retrieving and saving data. The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist.

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

The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. DataAdapter adds or refreshes rows in the DataSet to match those in the data source using the DataSet name, and creates a DataTable named "Table". From the following program you can understand how to use DataAdapter.Fill method in C# applications.




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());
			}
		}
	}
}






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.