Advertisement
Sabab

Linq

Nov 16th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1.   public  List<Item> AllItems()
  2.         {
  3.             SqlConnection connection=new SqlConnection(connectionString);
  4.  
  5.             try
  6.             {
  7.                 List<Item> itemList=new List<Item>();
  8.                 string selectQuery = "SELECT * FROM ItemTable";
  9.                 SqlCommand command=new SqlCommand(selectQuery,connection);
  10.                 connection.Open();
  11.  
  12.                 SqlDataReader reader = command.ExecuteReader();
  13.                
  14.                 while (reader.Read())
  15.                 {
  16.                     Item item=new Item();
  17.                     item.Name = reader["Name"].ToString();
  18.                     item.Price = Convert.ToDecimal(reader["Price"]);
  19.  
  20.                     itemList.Add(item);
  21.  
  22.                 }
  23.                 reader.Close();
  24.                 connection.Close();
  25.  
  26.                 return itemList;
  27.  
  28.             }
  29.             catch (Exception ex)
  30.             {
  31.                
  32.                 throw ex;
  33.             }
  34.    
  35.  
  36.  
  37.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement