You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. You are creating the data layer of the application. You write the following code segment. (Line numbers are included for reference only.)
01 public static SqlDataReader GetDataReader(string sql){
02 SqlDataReader dr;
03
04 return dr;
05 }
You need to ensure that the following requirements are met:
The SqlDataReader returned by the GetDataReader method can be used to retrieve rows from the database.
SQL connections opened within the GetDataReader method will close when the SqlDataReader is closed.
Which code segment should you insert at line 03?
A.
using (SqlConnection cnn=new SqlConnection(strCnn)){
try {
SqlCommand cmd =new SqlCommand(sql, cnn);
cnn.Open();
dr = cmd.ExecuteReader();
}
catch {
throw;
}
}
B.
SqlConnection cnn=new SqlConnection(strCnn);
SqlCommand cmd =new SqlCommand(sql, cnn);
cnn.Open();
try {
dr = cmd.ExecuteReader();
}
finally {
cnn.Close();
}
C.
SqlConnection cnn=new SqlConnection(strCnn);
SqlCommand cmd =new SqlCommand(sql, cnn);
cnn.Open();
try {
dr = cmd.ExecuteReader();
cnn.Close();
}
catch {
throw;
}
D.
SqlConnection cnn=new SqlConnection(strCnn);
SqlCommand cmd =new SqlCommand(sql, cnn);
cnn.Open();
try {
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch {
cnn.Close();
throw;
}