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