Monday, March 14, 2016

the old school .ExecuteScalar() way of writing a record with a sproc in C#

public void SaveFlight(Flight flight)
{
   using (SqlConnection sqlConnection = new SqlConnection(foo))
   {
      using (SqlCommand sqlCommand = new SqlCommand())
      {
         sqlCommand.CommandText = "dbo.AddFlight";
         sqlCommand.CommandType = CommandType.StoredProcedure;
         sqlCommand.Connection = sqlConnection;
         sqlCommand.Parameters.AddWithValue("@Duration", flight.Duration);
         sqlCommand.Parameters.AddWithValue("@DepartureTime", flight.DepartureTime);
         sqlCommand.Parameters.AddWithValue("@DepartureCity", flight.DepartureCity);
         sqlCommand.Parameters.AddWithValue("@DestinationCity",
               flight.DestinationCity);
         sqlConnection.Open();
         sqlCommand.ExecuteScalar();
      }
   }
}

No comments:

Post a Comment