C# Dapper – return single result

public static string GetImage(int restaurantId)
{
	using (SqlConnection conn = new SqlConnection(Common.ConnectionString))
	{
		conn.Open();
		const string query = "SELECT ImageURL FROM [Restaurant_Featured] WHERE RestaurantId = @RestaurantId";
		LEORestaurant_Featured featured = conn.Query<Restaurant_Featured>(query, new { RestaurantId = restaurantId }).SingleOrDefault();
		conn.Close();

		return featured != null && !string.IsNullOrWhiteSpace(featured.ImageURL) ? featured.ImageURL : string.Empty;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *