Friday, March 4, 2011

What is the difference between Execute Scalar and Execute Reader?

Execute Reader..
1.Returns a datareader with data.
2.It is done by command object.
3.It is readonly.
4.It executes only select command.
5. This is known as a forward-only retrieval of records.It uses your sql statement to read through the table from the first to the last.


Execute NonQuery..
1.It will not return any data.
2.It is used with insert and update.
3.It returns only the number of rows affected.

Execute Scaler..
1.It returns only one value.
2.That value will the first column first row value.

Execute Query..
1.Its for command objects.
2.It returns the value given by database through select statement.



Excute Reader:

Run a Query Command or a Stored procedure that selects records.
Excute Reader return one or more resultes.
con.open()
dim dr as sqldatareader()
cmd.excutereader()
'Result here
con.close()

U can access the selected records using the SqlDataReader object and use the Read to loop through them.you move the next result using next results method.

Excute NonQuery:-
un a Query Command or a Stored procedure that affects the specified table.This means anything but a query command. You normally use this method to issue an INSERT, UPDATE, DELETE, CREATE, and SET statement.

Con.Open()

nRecsAffected = cmd.ExecuteNonQuery();
Con.Close();
' check the record(s) affected here

ExecuteScalar:-

ExecuteScalar Expects to run a query command or more likely a stored procedure that returns data.However,This method is different from ExecuteReader in that it just makes available, as a scalar value,
the first column on the first row of the selected resultset.
c
Connection.Open();
Object o = cmd.ExecuteScalar(); cmd.Connection.Close();
// work on the scalar here
The method returns the value as a boxed object.




CommandPurpose
ExecuteReaderReturns a connected recordset. It is forward only and uneditable at the same time.

     cmd.CommandText = "select deptno, dname, loc from dept";
     Dim dr As sqlDataReader = cmd.ExecuteReader
     While dr.Read
          ' Process data as you would normally.
     End While
ExecuteScalarThis returns one value only, no recordsets.

     cmd.CommandText = "Select Name, DOB, from Emp where ID=1";
     Dim strName As string = cmd.ExecuteScalar.ToString
ExecuteXMLReaderReturns the recordset as a XML document.

     cmd.CommandText = "select deptno, dname, loc from dept";
     cmd.CommandText = "Select Name, DOB, Town from Emp FOR XML AUTO, ELEMENTS";
     Dim strName As string = cmd.ExecuteScalar.ToString

     dim reader as System.XML.XMLReader = cmd.ExecuteXMLReader

     Do While objXML.Read
          ' Do Some processing
     Loop

     objXML.Close
     con.Close
ExecuteNonQueryDoesn't return any recordsets, ideal for insert, update and delete queries where no recordsets are returned.

     cmd.CommandText = "Delete from Class where PupilHasLeft=true";
     cmd.ExecuteNonQuery

However, although records are returned, the number of affected records is.
da.Fill(DT)This populates a datatable or dataset from a select query.

     Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
     Dim dataSet As System.Data.DataTable = New System.Data.DataTable

     dataAdapter.SelectCommand = dbCommand
     dataAdapter.Fill(dataTable)

     For Each objDR in objDT.Rows
          ' Do Some Processing
     Next

No comments :

Post a Comment