SqlDataAdapter.Fill : SqlDataAdapter « System.Data.SqlClient « C# / C Sharp by API






SqlDataAdapter.Fill

  
using System;
using System.Data;
using System.Data.SqlClient;

class PopulateDataSetUsingSelect
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText =
      "SELECT TOP 5 ID, FirstName, LastName " +
      "FROM Employee " +
      "ORDER BY ID";

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
    mySqlDataAdapter.SelectCommand = mySqlCommand;

    DataSet myDataSet = new DataSet();

    mySqlConnection.Open();

    Console.WriteLine("Retrieving rows from the Employee table");
    int numberOfRows = mySqlDataAdapter.Fill(myDataSet, "Employee");
    Console.WriteLine("numberOfRows = " + numberOfRows);

    mySqlConnection.Close();

    DataTable myDataTable = myDataSet.Tables["Employee"];

    foreach (DataRow myDataRow in myDataTable.Rows)
    {
      Console.WriteLine("ID = " + myDataRow["ID"]);
      Console.WriteLine("First Name = " + myDataRow["FirstName"]);
      Console.WriteLine("Last Name = " + myDataRow["LastName"]);
    }
  }
}

   
    
  








Related examples in the same category

1.new SqlDataAdapter
2.SqlDataAdapter.DeleteCommand
3.SqlDataAdapter.InsertCommand
4.SqlDataAdapter.RowUpdated
5.SqlDataAdapter.RowUpdating
6.SqlDataAdapter.SelectCommand
7.SqlDataAdapter.SelectCommand.Parameters.Add
8.SqlDataAdapter.TableMappings
9.SqlDataAdapter.TableMappings.Add
10.SqlDataAdapter.Update
11.SqlDataAdapter.UpdateCommand