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






SqlDataAdapter.SelectCommand

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

class SelectIntoDataSet {
  public static void Main() {
    string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";

    SqlConnection mySqlConnection = new SqlConnection(connectionString);

    string selectString = "SELECT TOP 10 ID, FirstName, LastName FROM Employee ORDER BY ID";

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

    mySqlCommand.CommandText = selectString;

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();

    mySqlConnection.Open();

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

    mySqlConnection.Close();

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

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

   
    
  








Related examples in the same category

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