Friday 30 September 2011

What is the difference between ExecuteScalar and ExecuteNonQuery? What is ExecuteReader?


ExecuteScalar - Returns only one value after execution of the query. It returns the first field in the first row. This is very light-weight and is perfect when all your query asks for is one item. This would be excellent for receiving a count of records (Select Count(*)) in an sql statement, or for any query where only one specific field in one column is required.

ExecuteNonQuery - This method returns no data at all. It is used majorly with Inserts and Updates of tables. It is used for execution of DML commands.
Example:
SqlCommand cmd = new SqlCommand("Insert Into t_SomeTable Values('1','2')",con);
//note that con is the connection object
con.Open();
cmd.ExecuteNonQuery(); //The SQL Insert Statement gets executed
ExecuteReader - This method returns a DataReader which is filled with the data that is retrieved using the command object. This is known as a forward-only retrieval of records. It uses our SQL statement to read through the table from the first to the last record.
What is the difference between a DataReader and Dataset in ADO.NET?
A DataReader works in a connected environment, whereas DataSet works in a disconnected environment.

A DataReader object represents a forward only, read only access to data from a source. It implements IDataReader & IDataRecord interfaces. For example, The SQLDataReader class can read rows from tables in a SQL Server data source. It is returned by the ExecuteReader method of the SQLCommand class, typically as a result of a SQL Select statement. The DataReader class' HasRows property can be called to determine whether the DataReader retrieved any rows from the source. This can be used before using the Read method to check whether any data has been retrieved.
Example
Dim objCmd as New SqlCommand("Select * from t_Employees", objCon)
objCon.Open()
Dim objReader as SqlDataReader
objReader = objCom.ExecuteReader(CommandBehavior.CloseConnection)
If objReader.HasRows = True then
 Do While objReader.Read()
   ListBox1.Items.Add(objReader.GetString(0) & vbTab & objReader.GetInt16(1))
 Loop
End If
objReader.Close()
(NOTE: XmlReader object is used for Forward only Read only access of XML).

A DataSet represents an in-memory cache of data consisting of any number of inter-related DataTable objects. A DataTable object represents a tabular block of in-memory data. Further, a DataRow represents a single row of a DataTable object. A Dataset is like a mini-database engine, but its data is stored in the memory. To query the data in a DataSet, we can use a DataView object.

Example
Dim objCon as SqlConnection = New SqlConnection("server=(local);database=NameOfYourDb;user id=sa; password=;)
Dim da as New SqlDataAdapter
Dim ds as DataSet = New DataSet
da.SelectCommand.Connection = objCon 'The Data Adapter manages on its own, opening & closing of connection object
da.SelectCommand.CommandText = "Select * from t_SomeTable"
da.Fill(ds,"YourTableName")

Suppose you want to bind the data in this dataset to a gridview

Gridview1.DataSource = ds
Gridview1.DataMember = "YourTableName"
Gridview1.Databind()

No comments:

Post a Comment