nerdexam
Microsoft

70-516 · Question #38

Which one of these samples it the correct way to close the connection using Command Behavior?

The correct answer is B. SqlDataReader rdr = new SqlDataReader(). An alternative to explicitly closing the Connection is to pass CommandBehavior.CloseConnection to the ExecuteReader method to ensure that the associated connection is closed when the DataReader is closed. This is especially useful if you are returning a DataReader from a method…

Implementing Data Access

Question

Which one of these samples it the correct way to close the connection using Command Behavior?

Options

  • ASqlDataReader rdr = new SqlDataReader();
  • BSqlDataReader rdr = new SqlDataReader();
  • CSqlDataReader rdr = new SqlDataReader();
  • Dusing (SqlDataReader rdr = new SqlDataReader())

How the community answered

(32 responses)
  • A
    13% (4)
  • B
    78% (25)
  • C
    3% (1)
  • D
    6% (2)

Explanation

An alternative to explicitly closing the Connection is to pass CommandBehavior.CloseConnection to the ExecuteReader method to ensure that the associated connection is closed when the DataReader is closed. This is especially useful if you are returning a DataReader from a method and do not have control over the closing of the DataReader or associated connection. When you close the data reader and you use CommandBehavior.CloseConnection-the SQLconnection also closes

Topics

#SqlDataReader#CommandBehavior#connection management#using statement

Community Discussion

No community discussion yet for this question.

Full 70-516 Practice