PrepAway - Latest Free Exam Questions & Answers

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 Shared Function GetDataReader(sql As String) As SqlDataReader
02 Dim dr As SqlDataReader = Nothing
03
04 Return dr
05 End Function
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?

PrepAway - Latest Free Exam Questions & Answers

A.
Using cnn As New SqlConnection(strCnn)
Try
Dim cmd As New SqlCommand(sql, cnn)
cnn.Open()
dr = cmd.ExecuteReader()
Catch
Throw
End Try
End Using

B.
Dim cnn As New SqlConnection(strCnn)
Dim cmd As New SqlCommand(sql, cnn)

cnn.Open()
Try
dr = cmd.ExecuteReader()
Finally
cnn.Close()
End Try

C.
Dim cnn As New SqlConnection(strCnn)
Dim cmd As New SqlCommand(sql, cnn)
cnn.Open()
Try
dr = cmd.ExecuteReader()
cnn.Close()
Catch
Throw
End Try

D.
Dim cnn As New SqlConnection(strCnn)
Dim cmd As New SqlCommand(sql, cnn)
cnn.Open()
Try
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch
cnn.Close()
Throw
End Try

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