Which code segment should you insert at line 03?

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 – null;
03
03 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?

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 – null;
03
03 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.
SqlConnection cnn = new SqlConnectlon(strCnn);
SqlCommand cmd = new SqlCottmandfsql, cnn);
cnn.Open() ;
try {
dr = cmd.ExecuteReader();
cnn.Close();
{
catch {
throw;
}

B.
SqlConnection cnn = new SqlConnection(scrCnn) ;
SqlCommand cmd = new SqlCommandfsql, cnn);
cnn.Open () ;
try {
dr = cmd.ExecuceReader(CommandBehavior.CloseConnection);
{
catch {
cnn.Close();
throw;
}

C.
using (SqlConnection cnn = new SqlConnection(strCnn))
{
try {
SqlCommand cmd = new SqlCommand(sql, cnn);

cnn.Open();
dr = cmd.ExecuteReader(); ) catch
{
throw;
}}

D.
SqlConnection cnn = new SqlConnection(strCnn);
SqlCommand cmd = new SqlCommand(sql, cnn);
cnn.Open() ;
try
{
dr = cmd.ExecuteReader();
}
finally
{
cnn.Close() ;
}

Explanation:
CommandBehavior.CloseConnection When the command is executed, the associated Connection
object is closed when the associated DataReader object is closed.
CommandBehavior Enumeration
(http://msdn.microsoft.com/en-us/library/system.data.commandbehavior.aspx)
SqlCommand.ExecuteReader Method (CommandBehavior)
(http://msdn.microsoft.com/en-us/library/y6wy5a0f.aspx)



Leave a Reply 0

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