You create Microsoft Windows-based applications. You create an application that accesses data on a Microsoft SQL Server 2005 database. You write the following code segment. (Line numbers are included for reference only.)
01 private void LoadData()
02 {
04 cn.Open();
05 daProducts.Fill(ds);
06 daCategories.Fill(ds);
07 cn.Close();
09 }
The cn variable points to a SqlConnection object. The SqlConnection object will be opened almost every time this code segment executes. You need to complete this code segment to ensure that the application continues to run even if the SqlConnection object is open. You also need to ensure that the performance remains unaffected. What should you do?
A.
Add a Try block on line 03 along with a matching Catch block beginning on line 08 to handle the possible exception.
B.
Add a Try block on line 03 along with a matching Finally block beginning on line 08 to handle the possible exception.
C.
Add the following code to line 03.if (cn.ConnectionState!=ConnectionState.Open)
D.
Add the following code to line 03.if (cn.ConnectionState==ConnectionState.Closed)