How to control the command behavior to return a single row : Select « Database ADO.net « C# / C Sharp






How to control the command behavior to return a single row


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

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

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText ="SELECT ID, FirstName, LastName FROM Employee";

    mySqlConnection.Open();

    SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.SingleRow);

    while (mySqlDataReader.Read()){
      Console.WriteLine("mySqlDataReader[\" ID\"] = " +
        mySqlDataReader["ID"]);
      Console.WriteLine("mySqlDataReader[\" FirstName\"] = " +
        mySqlDataReader["FirstName"]);
      Console.WriteLine("mySqlDataReader[\" LastName\"] = " +
        mySqlDataReader["LastName"]);
    }

    mySqlDataReader.Close();
    mySqlConnection.Close();
  }
}
           
       








Related examples in the same category

1.Populate a DataSet object with a range of rows from a SELECT statement
2.how to execute a SELECT statement using a SqlCommand object