How to control the command behavior to return a single row : Select : Database C# Source Code


Custom Search

C# Source Code » Database » Select »

 

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







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» Select