Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

hjnntaao

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

ADO.NET - Succinctly populating a DataTable

From code supporting article at http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx
   1  
   2  
   3      public class DatabaseGateway
   4      {
   5          public DataTable QueryForDataTable(string expression)
   6          {
   7              using (IDatabaseConnection connection = new DatabaseConnection())
   8              {
   9                  DataTable table = new DataTable();
  10  
  11                  using (IDataReader reader = connection.CreateCommandFor(expression).ExecuteReader())
  12                  {
  13                      table.Load(reader);
  14                  }
  15  
  16                  return table;
  17              }
  18          }
  19      }
  20  
  21      public class DatabaseConnection : IDatabaseConnection
  22      {
  23          public IDbCommand CreateCommandFor(string dynamicSqlExpression)
  24          {
  25              IDbCommand command = underlyingConnection.CreateCommand();
  26              command.CommandType = CommandType.Text;
  27              command.CommandText = dynamicSqlExpression;
  28              return command;
  29          }
  30  
  31          public void Dispose()
  32          {
  33              underlyingConnection.Close();
  34              underlyingConnection.Dispose();
  35              underlyingConnection = null;
  36          }
  37  
  38  
  39          // ...
  40      }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS