Which code segment should you use?

You are creating a Windows Forms application by using the .NET Framework 3.5. You need to populate a list box control along with category names by using a DataReader control. Which code segment should you use?

You are creating a Windows Forms application by using the .NET Framework 3.5. You need to populate a list box control along with category names by using a DataReader control. Which code segment should you use?

A.
OleDbDataReader reader;
OleDbConnection cnnNorthwind = new OleDbConnection( connectionString);
cnnNorthwind.Open();
OleDbCommand cmdCategory = new OleDbCommand( “SELECT * FROM Categories”, cnnNorthwind);
reader = cmdCategory.ExecuteReader();
while (reader.Read())
{
lbCategories.Items.Add(reader[“CategoryName”]);
}
cnnNorthwind.close();

B.
OleDbDataReader reader;
OleDbConnection cnnNorthwind = new OleDbConnection connectionString);
cnnNorthwind.Open();
OleDbCommand cmdCategory = new OleDbCommand “SELECT * FROM Orders”, cnnNorthwind);
reader = cmdCategory.ExecuteReader();
while (reader.NextResult())
{
lbCategories.Items.Add(reader[“CategoryName”]);
}
cnnNorthwind.close();

C.
OleDbDataReader reader;
OleDbConnection cnnNorthwind = new OleDbConnection( connectionString);
cnnNorthwind.Open();
OleDbCommand cmdCategory = new OleDbCommand( “SELECT * FROM Orders”, cnnNorthwind);
reader = cmdCategory.ExecuteReader();
cnnNorthwind.Close();
while (reader.Read())
{
lbCategories.Items.Add(reader[“CategoryName”]);
}
cnnNorthwind.close();

D.
OleDbDataReader reader;
using (OleDbConnection cnnNorthwind = new OleDbConnection( connectionString))
{
cnnNorthwind.Open();
OleDbCommand cmdCategory = new OleDbCommand( “SELECT * FROM Orders”, cnnNorthwind);
reader = cmdCategory.ExecuteReader();
}
while (reader.Read())
{
lbCategories.Items.Add(reader[“CategoryName”]);
}
cnnNorthwind.close();



Leave a Reply 0

Your email address will not be published. Required fields are marked *