Advertisement
murkata86

Untitled

Oct 23rd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. public IEnumerable<T> FindAll<T>()
  2.         {
  3.             List<T> allData = new List<T>();
  4.  
  5.             using (connection = new SqlConnection(this.connectionString))
  6.             {
  7.                 this.connection.Open();
  8.  
  9.                 string query = $"SELECT * FROM {this.GetTableName(typeof(T))}";
  10.  
  11.                 string countQuery = $"SELECT COUNT(*) FROM {this.GetTableName(typeof(T))}";
  12.                
  13.                 SqlCommand countCommand = new SqlCommand(countQuery, this.connection);
  14.  
  15.                 int count = (int) countCommand.ExecuteScalar();
  16.  
  17.                 SqlCommand command = new SqlCommand(query, this.connection);
  18.  
  19.                 SqlDataReader reader = command.ExecuteReader();
  20.  
  21.                 using (reader)
  22.                 {
  23.                    
  24.                     for (int i = 0; i < count; i++)
  25.                     {
  26.                         allData.Add(this.CreateEntity<T>(reader));
  27.                     }
  28.                 }              
  29.             }
  30.  
  31.             return allData;
  32.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement