You need to define the connection life cycle of the DataAccessLayer class

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application contains the following code segment. (Line numbers are included for reference only.)
01 Class DataAccessLayer
02 Private Shared connString As String
03
04 …
05 Public Shared Function GetDataTable(command As String) As DataTable
06
07 …
08 End Function
09 End Class
You need to define the connection life cycle of the DataAccessLayer class. You also need to ensure
that the application uses the minimum number of connections to the database. What should you
do?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application contains the following code segment. (Line numbers are included for reference only.)
01 Class DataAccessLayer
02 Private Shared connString As String
03
04 …
05 Public Shared Function GetDataTable(command As String) As DataTable
06
07 …
08 End Function
09 End Class
You need to define the connection life cycle of the DataAccessLayer class. You also need to ensure
that the application uses the minimum number of connections to the database. What should you
do?

A.
Insert the following code segment at line 03.
Private Shared conn As New SqlConnection(connString)
Public Shared Sub Open()
conn.Open()
End Sub
Public Shared Sub Close()

conn.Close()
End Sub

B.
Insert the following code segment at line 03.
Private conn As New SqlConnection(connString)
Public Sub Open()
conn.Open() End Sub Public Sub Close()
conn.Close()
End Sub

C.
Replace line 01 with the following code segment.
Class DataAccessLayer
Implements IDisposable
Insert the following code segment to line 03.
Private conn As New SqlConnection(connString)
Public Sub Open()
conn.Open()
End Sub
Public Sub Dispose()
conn.Close()
End Sub

D.
Insert the following code segment at line 06.
Using conn As New SqlConnection(connString)
conn.Open()
End Using

Explanation:
One thing you should always do is to make sure your connections are always opened within a using
statement. Using statements will ensure that even if your application raises an exception while the
connection is open, it will always be closed (returned to the pool) before your request is complete.
This is very important, otherwise there could be connection leaks.



Leave a Reply 0

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