Review Questions

1. 

Which of the following is the appropriate connection string for logging onto a Microsoft SQL Server 6.5 database?

  1. myConn.ConnectionString = _   "Provider=MSSQL; Data Source=(local); " & _   "Initial Catalog=pubs" & _   "User ID=guest; Password=p5n7u!N"
  2. myConn.ConnectionString = _    "Data Source=(local); Initial Catalog=pubs" & _    "User ID=guest; Password=p5n7u!N"
  3. myConn.ConnectionString = _    "Provider=MSSQL; Data Source=pubs; " & _    "Initial Catalog=(local) " & _    "User ID=guest; Password=p5n7u!N"
  4. myConn.ConnectionString = _    "Provider=MSSQL; Data Source=pubs; " & _    "Initial Catalog=(local) " & _    "User ID=guest; Password=p5n7u!N"

aversion 6.5 and older of microsoft sql server must use the oledb data providers; therefore, they need to specify a provider name in the connection string. the data source should be set to the computer name of the database server (or local machine), and the initial catalog is the name of the database.

2. 

Your Windows forms application uses Windows Integrated Security and allows users of your application to connect to the SQL Server database by using their own Windows username. Users sometimes report that database operations are very slow. What action might improve data access time?

  1. Change the SQL Server security mode to mixed mode.

  2. Allow your application to log in as the system administrator.

  3. Create a single application login so that a single connection pool can serve all users of your application.

  4. Rewrite your application’s SQL queries.

cwhen users log into the database with unique login names, connection pooling cannot work efficiently because each user will get their own connection pool. changing to a single application login will enable users to get existing connections from the pool, which is quicker than creating new connections for each user. changing sql server to mixed mode will not improve performance and will introduce new security considerations, as will allowing your application to log in as an administrator. rewriting your sql queries might or might not have any effect on application performance.

3. 

Your application will be using ADO.NET Command objects to call stored procedures. Which Command property settings should you use?

  1. Set the CommandType property to StoredProcedure and the CommandText property to the name of the procedure.

  2. Set the CommandText property to StoredProcedure and the CommandType property to the name of the procedure.

  3. Set the CommandType property to Database and the CommandText property to StoredProcedure.

  4. Set the CommandType property to StoredProcedure and the CommandText property to the value of the input parameter.

athe commandtype property indicates what kind of operation the command will be performing. there are three valid values: text (a sql statement provided in your code), storedprocedure , or tabledirect . the commandtext property is a string value that is a sql statement provided in your code, the name of a stored procedure, or the name of a table. parameter values are handled by the command.parameters collection.

4. 

You are using an ADO.NET Command object to run a SQL query that requests a count of rows in a database table. Which command method should you use?

  1. ExecuteNonQuery

  2. ExecuteReader

  3. ExecuteXMLReader

  4. ExecuteScalar

duse the executescalar method when running a query that returns a single value. use the executereader method when running a query that returns rows of data. use the executenonquery method when running a query such as a sql insert , update , or delete statement. the executexmlreader returns an xml document object and is for use only with the sqlclient data provider and sql server 2000 for xml queries.

5. 

You are using an ADO.NET Command object to run a SQL query that will delete a row in the database. Which command method should you use?

  1. ExecuteNonQuery

  2. ExecuteReader

  3. ExecuteXMLReader

  4. ExecuteScalar

ause the executenonquery method when running a query such as a sql insert , update , or delete statement. use the executescalar method when running a query that returns a single value. use the executereader method when running a query that returns rows of data. the executexmlreader returns an xml document object and is for use only with the sqlclient data provider and sql server 2000 for xml queries.

6. 

You have created a DataReader object to read customer information from the database. What instruction should you use to retrieve the customer’s name from the first column in a DataReader’s resultset?

  1. myString = myReader.GetChars(0)

  2. myString = myReader.GetChars(1)

  3. myString = myReader.GetString(0)

  4. myString = myReader.GetString(1)

cthe getstring method should be used because you know that the field that contains the customer name is defined as a string or character data type. the getchars method is used to read database columns that hold large binary data objects (blob). the first column the in the datareader s resultset is at ordinal position zero (0), not 1.

7. 

In order to read all the rows from a DataReader, which method should you call?

  1. myReader.NextResult()

  2. myReader.MoveNext()

  3. myReader.Read()

  4. myReader.GetValues()

cthe read method is used to advance the datareader to the next row of data. the nextresult method is used when several sql queries were run as a batch and there are multiple resultsets in a single datareader. the movenext method was used with older versions of the ado recordset and is not used in ado.net. the getvalues method is for retrieving column data.

8. 

You need to be sure that the database connection is closed immediately when its associated DataReader object is closed by the consumer. How can you most easily accomplish this?

  1. With the DataReader.Dispose method.

  2. With the DataReader.Close method.

  3. When the DataReader is created by the Command.ExecuteReader method, pass a parameter called CommandBehavior.SequentialAccess.

  4. When the DataReader is created by the Command.ExecuteReader method, pass a parameter called CommandBehavior.CloseConnection.

dalthough you can write code to create this behavior, it is most easily accomplished by simply setting the commandbehavior.closeconnection parameter when creating the datareader.

9. 

When creating an ASP.NET web application, how can you quickly display information from a DataReader, called myReader, in a Web Forms DataGrid control?

  1. Set the DataSource property of the DataGrid to reference the myReader and then call the DataGrid.DataBind method.

  2. Set the DataReader property of the DataGrid to reference the myReader and then call the DataGrid.DataBind method.

  3. Set up a loop to read through the DataReader and assign values to the rows and columns of the DataGrid.

  4. Set up special template columns for the DataGrid and then use the GetDataType methods of the DataReader to display each row of data.

aasp.net web forms controls are able to use automatic data binding to access data through a datareader. windows forms controls cannot do this. just set the datasource property of the datagrid to reference the datareader instance and call the datagrid.databind method. you do not need to loop through the rows in the datareader or write code to work with individual column values.

10. 

You are using an ADO.NET Command object to run a SQL query that will update selected rows in the database, based on the criteria specified in your SQL statement’s WHERE clause. Your call to the ExecuteNonQuery method looks like this:

x = myCommand.ExecuteNonQuery()

What will the variable x contain after the query is run?

  1. -1

  2. True or False, indicating whether or not any errors occurred while processing the data

  3. A status code from the data base server

  4. The number of rows that were updated

dthe executenonquery method returns an integer value showing the number of records that were affected by the query. when working with a datareader, the recordsaffected property always returns -1 for sql select statements. error information and status codes are accessed through the exception and error objects.

11. 

You are using an ADO.NET Command object to run a SQL query that will return a single calculated value. Your call to the ExecuteScalar method looks like this:

x = myCommand.ExecuteScalar()

What data type should you use when you declare your variable named x?

  1. Integer

  2. Object

  3. Variant

  4. Decimal

bbecause the executescalar method can return different types of data, it returns an object data type. you can then write code to convert to a more specific data type. the data type of variant was used in visual basic 6 and is not one of the .net framework data types.

12. 

Your procedure needs to perform two separate database queries. You need to debit an amount in the first database table and credit that amount in another table. You want to make sure that both operations are successful. If one of the instructions fails, no partial changes should be written to the database. Which ADO.NET objects should you use?

  1. Use the Connection object’s BeginTrans, CommitTrans, and Rollback methods.

  2. Instantiate a new Transaction object and call its methods to commit or roll back the transaction.

  3. Use the Connection object to create a new Transaction object and then use methods of the Transaction object to commit or roll back the transaction.

  4. Create a new Transaction object and add it to the Connection.Transactions collection.

cthe ado.net transaction object cannot be instantiated with the new keyword. it is created by the connection.begintransaction method. after the object is created, you can call methods of the transaction object to commit or roll back the transaction. in older versions of ado, the connection object was used to control transactions and did have begintrans , committrans , and rollback methods. there is no connection.transactions collection.

13. 

You are using the ADO.NET Transaction object to coordinate database operations in your code. You would like to make a setting indicating to the database server that you would like the highest level of database locking to be applied while your transaction is running. Which value should you use for the Transaction.IsolationLevel property?

  1. ReadCommitted

  2. Serializable

  3. ReadUncommitted

  4. RepeatableRead

b serializable provides the highest level of isolation and ensures that no other operations can change or even read the data until your transaction is committed. the other settings provide lower levels of protection.

14. 

You are creating error handling for your ADO.NET application that will use the SqlClient data provider. You are interested in processing only data access errors with this Catch block. How should you specify the Catch block portion of your error handler?

  1. Catch ex As Exception

  2. Catch ex As SqlException

  3. Catch ex As SqlError

  4. Catch ex as SqlException.Errors

bspecify a sqlexception object in the catch block. inside the catch block, you can then access the sqlerror objects that make up the sqlexception.errors collection. if you specify system.exception in the catch block, you will receive all types of runtime errors.

15. 

Code in your error handler does not access the SqlException.Errors collection to read error messages, but rather reads the Message property directly from the SqlException object. What effect does this have on your application?

  1. You will not see any error messages.

  2. You will the same message as the first SqlError object in the Errors collection.

  3. You will see a message warning you to read the Errors collection.

  4. You will see a generic message.

bthe sqlexception object s property values will be the same as the first sqlerror object in the errors collection.

Answers

1. 

A Version 6.5 and older of Microsoft SQL Server must use the OleDb data providers; therefore, they need to specify a provider name in the connection string. The Data Source should be set to the computer name of the database server (or local machine), and the Initial Catalog is the name of the database.

2. 

C When users log into the database with unique login names, connection pooling cannot work efficiently because each user will get their own connection pool. Changing to a single application login will enable users to get existing connections from the pool, which is quicker than creating new connections for each user. Changing SQL Server to mixed mode will not improve performance and will introduce new security considerations, as will allowing your application to log in as an administrator. Rewriting your SQL queries might or might not have any effect on application performance.

3. 

A The CommandType property indicates what kind of operation the command will be performing. There are three valid values: Text (a SQL statement provided in your code), StoredProcedure, or TableDirect. The CommandText property is a string value that is a SQL statement provided in your code, the name of a stored procedure, or the name of a table. Parameter values are handled by the Command.Parameters collection.

4. 

D Use the ExecuteScalar method when running a query that returns a single value. Use the ExecuteReader method when running a query that returns rows of data. Use the ExecuteNonQuery method when running a query such as a SQL INSERT, UPDATE, or DELETE statement. The ExecuteXMLReader returns an XML document object and is for use only with the SqlClient data provider and SQL Server 2000 FOR XML queries.

5. 

A Use the ExecuteNonQuery method when running a query such as a SQL INSERT, UPDATE, or DELETE statement. Use the ExecuteScalar method when running a query that returns a single value. Use the ExecuteReader method when running a query that returns rows of data. The ExecuteXMLReader returns an XML document object and is for use only with the SqlClient data provider and SQL Server 2000 FOR XML queries.

6. 

C The GetString method should be used because you know that the field that contains the customer name is defined as a string or character data type. The GetChars method is used to read database columns that hold large binary data objects (BLOB). The first column the in the DataReader’s resultset is at ordinal position zero (0), not 1.

7. 

C The Read method is used to advance the DataReader to the next row of data. The NextResult method is used when several SQL queries were run as a batch and there are multiple resultsets in a single DataReader. The MoveNext method was used with older versions of the ADO recordset and is not used in ADO.NET. The GetValues method is for retrieving column data.

8. 

D Although you can write code to create this behavior, it is most easily accomplished by simply setting the CommandBehavior.CloseConnection parameter when creating the DataReader.

9. 

A ASP.NET Web Forms controls are able to use automatic data binding to access data through a DataReader. Windows Forms controls cannot do this. Just set the DataSource property of the DataGrid to reference the DataReader instance and call the DataGrid.DataBind method. You do not need to loop through the rows in the DataReader or write code to work with individual column values.

10. 

D The ExecuteNonQuery method returns an integer value showing the number of records that were affected by the query. When working with a DataReader, the RecordsAffected property always returns -1 for SQL SELECT statements. Error information and status codes are accessed through the Exception and Error objects.

11. 

B Because the ExecuteScalar method can return different types of data, it returns an Object data type. You can then write code to convert to a more specific data type. The data type of Variant was used in Visual Basic 6 and is not one of the .NET Framework data types.

12. 

C The ADO.NET Transaction object cannot be instantiated with the New keyword. It is created by the Connection.BeginTransaction method. After the object is created, you can call methods of the Transaction object to commit or roll back the transaction. In older versions of ADO, the Connection object was used to control transactions and did have BeginTrans, CommitTrans, and Rollback methods. There is no Connection.Transactions collection.

13. 

B Serializable provides the highest level of isolation and ensures that no other operations can change or even read the data until your transaction is committed. The other settings provide lower levels of protection.

14. 

B Specify a SqlException object in the Catch block. Inside the Catch block, you can then access the SqlError objects that make up the SqlException.Errors collection. If you specify System.Exception in the Catch block, you will receive all types of runtime errors.

15. 

B The SqlException object’s property values will be the same as the first SqlError object in the Errors collection.



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net