Question 28: Explain the properties and methods of Command Object.
Answer: The command object is one of the basic components of ADO .NET.
Associated Properties of SqlCommand class
Property | Type of Access | Description |
Connection | Read/Write | The SqlConnection object that is used by the command object to execute SQL queries or Stored Procedure. |
CommandText | Read/Write | Represents the T-SQL Statement or the name of the Stored Procedure. |
CommandType | Read/Write | This property indicates how the CommandText property should be interpreted. The possible values are: 1. Text (T-SQL Statement) 2. StoredProcedure (Stored Procedure Name) 3. TableDirect |
CommandTimeout | Read/Write | This property indicates the time to wait when executing a particular command. Default Time for Execution of Command is 30 Seconds. The Command is aborted after it times out and an exception is thrown. |
Now, let us have a look at various execute methods that can be called from a Command Object.
Property | Description |
ExecuteNonQuery | This method executes the command specifies and returns the number of rows affected. |
ExecuteReader | The ExecuteReader method executes the command specified and returns an instance of instance of SqlDataReader class. |
ExecuteScalar | This method executes the command specified and returns the first column of first row of the result set. The remaining rows and column are ignored |
ExecuteXMLReader | This method executes the command specified and returns an instance of XmlReader class. This method can be used to return the result set in the form of an XML document |
See for more detail:
Question 15: Describe the DataView in ADO.NET?
Answer: A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. A DataView provides a dynamic view of data whose content, ordering, and membership reflect changes to the underlying DataTable as they occur. This is different from the Select method of the DataTable, which returns a DataRow array from a table per particular filter and/or sort order and whose content reflects changes to the underlying table, but whose membership and ordering remain static. The dynamic capabilities of the DataView make it ideal for data-binding applications.
How we can create a DataView
There are two ways to create a DataView. You can use the DataView constructor, or you can create a reference to the DefaultView property of the DataTable. The DataView constructor can be empty, or will also take either a DataTable as a single argument, or a DataTable along with filter criteria, sort criteria, and a row state filter.
See for more detail: