title: Database Best-Practices for Beginners
body: <p>So, I am a fairly new programmer working towards an undergraduate Comp Sci degree with a very small amount of work experience. In looking for internship-type jobs for my program, I have noticed that what I've heard from several profs -- "working with databases makes up 90% of all modern computer science jobs" -- looks like it is actually true. However, my program doesn't really have any courses with databases until 3rd year, so I'm trying to at least learn some things myself in the mean time.</p>
  
  <p>I've seen very little on SO and the internet in general for somebody like myself. There seem to be tons of tutorials on the mechanics of how to read and write data in a database, but little on the associated best practices. To demonstrate what I am talking about, and to help get across my actual question, here is what can <a href="http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx" rel="noreferrer">easily be found on the internet</a>:</p>
  
  <pre><code>public static void Main ()
  {
      using (var conn = new OdbcConnection())
      {
          var command = new OdbcCommand();
          command.Connection = conn;
          command.CommandText = "SELECT * FROM Customer WHERE id = 1";
          var dbAdapter = new OdbcDataAdapter();
          dbAdapter.SelectCommand = command;
          var results = new DataTable();
          dbAdapter.Fill(results);
      }
  
      // then you would do something like
      string customerName = (string) results.Rows[0]["name"]; 
  }
  </code></pre>
  
  <p>And so forth. This is pretty simple to understand but obviously full of problems. I started out with code like this and quickly started saying things like "well it seems dumb to just have SQL all over the place, I should put all that in a constants file." And then I realized that it was silly to have those same lines of code all over the place and just put all that stuff with connection objects etc inside a method:</p>
  
  <pre><code>public DataTable GetTableFromDB (string sql)
  {
      // code similar to first sample
  }    
  
  string getCustomerSql = String.Format(Constants.SelectAllFromCustomer, customerId);
  DataTable customer = GetTableFromDB(getCustomerSql);
  string customerName = (string) customer.Rows[0]["name"];
  </code></pre>
  
  <p>This seemed to be a big improvement. Now it's super-easy to, say, change from an OdbcConnection to an SQLiteConnection. But that last line, accessing the data, still seemed awkward; and it is still a pain to change a field name (like going from "name" to "CustName" or something). I started reading about <a href="http://msdn.microsoft.com/en-us/library/aa581776.aspx" rel="noreferrer">using typed Data sets or custom business objects</a>. I'm still kind of confused by all the terminology, but decided <a href="http://www.informit.com/articles/article.aspx?p=1273361" rel="noreferrer">to look into it anyway.</a> I figure that it is stupid to rely on a shiny Database Wizard to do all this stuff for me (like in the linked articles) before I actually learn what is going on, and why. So I took a stab at it myself and started getting things like:</p>
  
  <pre><code>public class Customer
  {
      public string Name {get; set;}
      public int Id {get; set;}
  
      public void Populate ()
      {
          string getCustomerSql = String.Format(Constants.SelectAllFromCustomer, this.Id);
          DataTable customer = GetTableFromDB(getCustomerSql);
          this.Name = (string) customer.Rows[0]["name"]; 
      }
  
      public static IEnumerable&lt;Customer&gt; GetAll()
      {
          foreach ( ... ) { 
              // blah blah
              yield return customer;
          }
      }
  }
  </code></pre>
  
  <p>to hide the ugly table stuff and provide some strong typing, allowing outside code to just do things like</p>
  
  <pre><code>var customer = new Customer(custId);
  customer.Populate();
  string customerName = customer.Name;
  </code></pre>
  
  <p>which is really nice. And if the Customer table changes, changes in the code only need to happen in one place: inside the <code>Customer</code> class.</p>
  
  <p>So, at the end of all this rambling, my question is this. Has my slow evolution of database code been going in the right direction? And where do I go next? This style is all well and good for small-ish databases, but when there are tons of different tables, writing out all those classes for each one would be a pain. I have heard about software that can generate that type of code for you, but am kind of still confused by the DAL/ORM/LINQ2SQL/etc jargon and those huge pieces of software are kind of overwhelming. I'm looking for some good not-overwhelmingly-complex resources that can point me in the right direction. All I can find on this topic are complex articles that go way over my head, or articles that just show you how to use the point-and-click wizards in Visual Studio and such. Also note that I'm looking for information on working with Databases in code, not information on Database design/normalization...there's lots of good material on that out there.</p>
  
  <p>Thanks for reading this giant wall of text.</p>