text: Q:
  
  Must I delete my SQL class or improve it? C#
  
  I have a class that stores the code snippet that I usually use for SQL queries. 
  Example, I use this method to fill datagridviews.Datasource with things like products, providers, clients, and any data that I need in a table:
  public DataTable GetDataTable(string query)
  {
      using (SqlConnection _connection = new SqlConnection(ConnectionString))
      {
          _connection.Open();
  
          using (SqlDataAdapter adapter = new SqlDataAdapter(query, _connection))
          {
              DataTable dt = new DataTable();
              adapter.Fill(dt);
  
              return dt;
          }
      }
  }
  
  I have similar things for commands and readers. The only problem was that with readers I was populating a list<string> with the first record in a SqlDataReader, so I had to run readers outside of class to deal with things like combo boxes. Trying to fix this problem, I started to think that it might be better to just put the code for the queries where I need it and not create a new ExecuteReader method to run the Data.SqlClient ExecuteReader method.
  I don't know if this class is a good idea or if I am wasting time trying to improve it and there are different and better ways to deal with this kind of thing. What do you think about?
  
  A:
  
  If you need a method that returns a typed list with data from the database, you can do this:
  public List<T> GetList<T>(string query, Func<SqlDataReader, T> func)
  {
      var list = new List<T>();
  
      using (var connection = new SqlConnection(connectionString))
      {
          connection.Open();
  
          using (var sqlCommand = new SqlCommand(query, connection))
          using (var reader = sqlCommand.ExecuteReader())
          {
              while (reader.Read())
              {
                  var item = func(reader);
                  list.Add(item);
              }
          }
      }
  
      return list;
  }
  
  That is, set the method for creating and filling in the object as a lambda function.
  Use it like this:
  var departments = GetList<Department>(
      "select Id, Name from Department",
      reader => new Department(reader.GetInt32(0), reader.GetString(1)) // using constructor
  );
  
  var orders = GetList<Order>(
      "select Id, Price, Date from Order",
      reader => new Order // using properties
      {
          Id = reader.GetInt32(0),
          Price = reader.GetDecimal(1),
          Date = reader.GetDateTime(2)
      }
  );
  
  And so on.
  
  