Skip to content

Instantly share code, notes, and snippets.

@lohithgn
Created June 18, 2013 19:55
Show Gist options
  • Save lohithgn/5808701 to your computer and use it in GitHub Desktop.
Save lohithgn/5808701 to your computer and use it in GitHub Desktop.
[OperationContract]
public List<ProductModel> GetTop10ProductsByPrice()
{
var prodList = new List<ProductModel>();
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnection"] as string;
string query = "SELECT TOP 10 ProductID, Name, UnitPrice FROM Product ORDER BY UnitPrice DESC";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
command.CommandType = System.Data.CommandType.Text;
var reader = command.ExecuteReader();
if (reader != null)
while (reader.Read())
{
var cust = new ProductModel
{
ProductID = reader.GetInt32(0),
ProductName = reader.GetString(1),
Price = reader.GetDecimal(2)
};
prodList.Add(cust);
}
return prodList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment