Which code segment should you insert at line 03?

You use Microsoft .NET Framework 4.0 to develop an application.
You write the following code to update data in a Microsoft SQL Server 2008 database.
(Line numbers are included for reference only.)

01 private void ExecuteUpdate(SqlCommand cmd, string connString, string updateStmt)
02 {
03 …
04 }

You need to ensure that the update statement executes and that the application avoids connection leaks.
Which code segment should you insert at line 03?

You use Microsoft .NET Framework 4.0 to develop an application.
You write the following code to update data in a Microsoft SQL Server 2008 database.
(Line numbers are included for reference only.)

01 private void ExecuteUpdate(SqlCommand cmd, string connString, string updateStmt)
02 {
03 …
04 }

You need to ensure that the update statement executes and that the application avoids connection leaks.
Which code segment should you insert at line 03?

A.
SqlConnection conn = new SqlConnection(connString);
conn.Open();
cmd.Connection = conn;
cmd.CommandText = updateStmt;
cmd.ExecuteNonQuery();
cmd.Connection.Close() ;

B.
using (SqlConnection conn = new SqlConnection(connString))
{
cmd.Connection = conn;
cmd.CommandText = updateStmt;
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}

C.
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open() ;
cmd.Connection = conn;
cmd.CommandText = updateStmt;
cmd.ExecuteNonQuery() ;
}

D.
SqlConnection conn = new SqlConnection(connString);
conn.Open();
cmd.Connection = conn;
cmd.CommandText = updateStmt;
cmd.ExecuteNonQuery();

Explanation:
http://www.w3enterprises.com/articles/using.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx



Leave a Reply 0

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