title: Generic Method to create Parameterized SQL from a Class
body: <p>I am working with a Database with 200+ tables. I have been able to auto generate classes based on the Table structure. I can also auto generate insert statements using a Data Table via a foreach loop like this:</p>
  
  <pre><code>foreach (DataColumn col in table.Columns)
  {
      colnames.Add("[" + col.ColumnName + "]");
  }
  </code></pre>
  
  <p>However my goal is to create Parameterized SQL Statements from the definition of the class itself and I am drawing a blank.</p>
  
  <p>For example: </p>
  
  <p>Class Declaration:</p>
  
  <pre><code>public class clsCaseWorker
  {
      public string CaseWorkerId{ get; set; }
      public string CaseWorkerFirstName{ get; set; }
      public string CaseWorkerLastName{ get; set; }
      public string CaseWorkerMiddleName{ get; set; }
      ...
  </code></pre>
  
  <p>Should generate a Method/ Parameterized query like this (Note Class Name will Map to Table Name)</p>
  
  <pre><code>public clsSavedInfo SaveCaseWorkerInfo(clsCaseWorker CaseWorker)
  {
      string saveCaseWorker;
      saveCaseWorker = "insert into CaseWorker ";
      saveCaseWorker += "([CaseWorkerFirstName], [CaseWorkerLastName], ";
      saveCaseWorker += "[CaseWorkerMiddleName], ...";
      saveCaseWorker += "Values(@cw_FirstName, @cw_LastName, @cw_MiddleName, @cw_CellPhone, @cw_HomePhone, @cw_Address, @cw_Tribe, @cw_TribeName, ";
      saveCaseWorker += "@cw_District, @cw_Title19, @cw_Title19Address, @cw_StateProgram, @cw_StateProgramText) SELECT CaseWorkerID FROM CaseWorker WHERE CaseWorkerID = @@IDENTITY";
  
      SqlCommand cmd = new SqlCommand(saveCaseWorker, con);
  
      cmd.Parameters.AddWithValue("@cw_FirstName", CaseWorker.CaseWorkerFirstName);
      cmd.Parameters.AddWithValue("@cw_LastName", CaseWorker.CaseWorkerLastName);
      cmd.Parameters.AddWithValue("@cw_MiddleName", CaseWorker.CaseWorkerMiddleName);
      ...
  </code></pre>
  
  <p>Any suggestion that points me to the right direction will be much appreciated.</p>