Talk:DataReader

Latest comment: 11 years ago by 5.10.169.18 in topic Sample code suggestion

Sample code suggestion edit

I suggest the following example (please note the additional using for mycommand and avoiding string concatenation):

void DataTest()
{
  using (SqlConnection conn1 = new SqlConnection(...))
  {
    conn1.Open();

    using (SqlCommand mycommand = new SqlCommand("Select * From someTable", conn1))
    using (SqlDataReader myreader = mycommand.ExecuteReader())
    {
      if(myreader == null)
      {
        return;
      }

      while(myreader.Read())
      {
        Console.WriteLine(string.Format("{0}:{1}", myreader.GetValue(0), myreader.GetTypeName(0));
      }
    }
  }
}

What do other's think? --Blue4k (talk) 17:49, 23 December 2010 (UTC)Reply

I like it! Much clearer! Wikipedia:Be bold 5.10.169.18 (talk) 14:36, 6 February 2013 (UTC)Reply