title: Class to DataSet / DataSet to class
body: <p>Right now I'm trying to create a database handler that handles databases dynamically. Say I have a class of fieldtypes "String, int, bool, String" and want to turn this class into a table and all the fieldtypes into "fields" of a dataset?</p>
  
  <p>How can I do that?</p>
  
  <p>Also, can I create a few classes that inherits: "System.Data.DataSet", "System.Data.DataTable", "System.Data.DataRow", and some sort of Adapter to handle it.</p>
  
  <p>I know when you go into design mode and create a dataset the code is really hard to make out when you look at it. But shouldn't it be possible by using these objects and creating classes that handle them to be able to "create" database dynamically. It wouldn't get a "Designer View" but, Database.AddTable(new Table("Field1", "Field2, "Field3")) should do the same as you do graphically in Designer mode.</p>
  
  <p>Any ideas?</p>
  
  <p>The main idea is that it's flexible and I can take whatever class and turn it into a table of rows with each object of this class' field values as database fields.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This is just a simple class that I made in the last hour, am I thinking the right way? <strong>Still need to be able to add DataSet to the *.mdf / *.sdf file, how do I do that?</strong></p>
  
  <pre><code>public class DataAccess
  {
  
      SqlConnection connection;
      DataSet dataSet;
  
      public DataAccess(String databaseName, String connectionStr)
      {
          dataSet = new DataSet(databaseName);
          connection = new SqlConnection(connectionStr);
      }
  
      public void AddTable&lt;T&gt;(String tableName)
      {
          dataSet.Tables.Add(new DBTable&lt;T&gt;(tableName));
      }
  
      public void AddRow&lt;T&gt;(String tableName, T row)
      {
          ((DBTable&lt;T&gt;)dataSet.Tables[tableName]).AddRow&lt;T&gt;(row);
      }
  
      public List&lt;C&gt; GetRows&lt;T, C&gt;(String tableName, String columnName)
      {
          return ((DBTable&lt;T&gt;)dataSet.Tables[tableName]).GetRow&lt;C&gt;(columnName);
      }
  
      public String GetXML()
      {
          return dataSet.GetXml();
      }
  
      #region METHOD PROPERTIES
  
      public int ColumnCount(String tableName)
      {
          for (int i = 0; i &lt; dataSet.Tables.Count; i++)
          {
              if (dataSet.Tables[i].TableName == tableName)
              {
                  return dataSet.Tables[i].Columns.Count;
              }
          }
  
          return 0;
      }
  
      #endregion
  
  
  }
  
  public class DBTable&lt;T&gt; : System.Data.DataTable
  {
      public DBTable(String tableName) : base(tableName)
      {
  
          PropertyInfo[] properties = typeof(T).GetProperties();
  
          for (int i = 0; i &lt; properties.Length; i++)
          {
              try
              {
                  AddColumn(properties[i].Name, properties[i].PropertyType);
              }
              catch { }
          }
  
      }
  
      private void AddColumn(String name, Type t)
      {
          this.Columns.Add(new DataColumn(name, t, null, MappingType.Attribute));
      }
  
      public void AddRow&lt;T&gt;(T obj)
      {
          PropertyInfo[] properties = typeof(T).GetProperties();
  
          if (properties.Length == this.Columns.Count)
          {
  
              bool valid = true;
  
              for (int i = 0; i &lt; properties.Length; i++)
              {
                  if (properties[i].PropertyType != this.Columns[i].DataType)
                  {
                      valid = false;
                  }
              }
  
              if (valid)
              {
                  object[] p = new object[this.Columns.Count];
  
                  for (int i = 0; i &lt; properties.Length; i++)
                  {
                      p[i] = properties[i].GetValue(obj, null);
                  }
  
                  this.Rows.Add(p);
              }
          }
      }
  
      public List&lt;T&gt; GetRow&lt;T&gt;(String columnName)
      {
  
          List&lt;T&gt; objects = new List&lt;T&gt;();
  
          for (int i = 0; i &lt; this.Rows.Count; i++)
          {
              objects.Add((T)this.Rows[i][this.Columns[columnName]]);
          }
  
          return objects;
  
      }
  }
  </code></pre>