PrepAway - Latest Free Exam Questions & Answers

Which code segment should you recommend?

You create Web-based client applications. You are reviewing a Web application page that populates the list of all employees for your company. The following code segment loads the list of employees from a database.
You analyze the code segment. You find that the database connection fails to close properly when the GetEmployees method throws an exception. You need to recommend a change in the code segment to ensure that every possible code path closes the database connection. Which code segment should you recommend?

PrepAway - Latest Free Exam Questions & Answers

A.
// Create the connection and open it
using (DbConnection conn = factory.CreateConnection())
{
conn.ConnectionString = connString.ConnectionString;
conn.Open();
// Get the employees. The connection to the database
// is given as parameter
lstEmployees = GetEmployees(conn);
}

B.
// Create the connection and open it
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = connString.ConnectionString;
conn.Open();
// Get the employees. The connection to the database is
// given as parameter
lstEmployees = GetEmployees(conn);
if (lstEmployees == null) {
conn.Dispose();
} else {
conn.Close();
}

C.
HandleCollector coll =
new HandleCollector(“Connections”, 0, 5);
// Create the connection and open it
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = connString.ConnectionString;
conn.Open();
coll.Add();
// Get the employees. The connection to the database is
// given as parameter
lstEmployees = GetEmployees(conn);
// Close the connection to the employee data store
Conn.Close();
Coll.Remove();

D.
// Create the connection and open it
using (IDisposable factory = DbProviderFactories.GetFactory(“System.Data.SqlClient”) as IDisposable) {
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = connString.ConnectionString;
conn.Open();
// Get the employees. The connection to the database
// is given as parameter
lstEmployees = GetEmployees(conn);
}


Leave a Reply