473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ExecuteReader,ExecuteScalar or ExecuteNonQuery

Hello!

If you do an update in the database you will use ExecuteNonQuery but
the database update will work just as good if you instead use ExecuteReader
or executeScalar.

So my question is how is this possible ?
So it seems to me that you can use whichever you like ?

//Tony
Aug 26 '08 #1
2 2898
On Aug 26, 5:38*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Hello!

If you do an update in the database you will use ExecuteNonQuery but
the database update will work just as good if you instead use ExecuteReader
or executeScalar.

So my question is how is this possible ?
So it seems to me that you can use whichever you like ?

//Tony
Hi,
You can do several things to the database, like querying, or updating.
Querying is used when you need data from database, so ado.net have to
provide a way for you to retrieve the data fetched by database engine,
which is the IDataReader returned by ExecuteReader. You can iterate
through the IDataReader to get every record you need.
Sometimes what you want is not records, but a single value, such as
the count of records in table "User". This is what ExecuteScalar is
designed for.
Updating is like you already have the data (maybe you got it last time
you call ExecuteReader), and you have done some modifications to it,
and then you want to save these modifications back to the database
(remember what you got is a copy of the data in database, so you have
to explicitly update the database to save any change). Now that you
already have the data, you only need to save them, don't need to load
them again, so you don't need a IDataReader, this is when you should
use ExecuteNonQuery.

Sometimes you can use anyone of these three methods, but it makes more
sense when you've chosen the best one.
Aug 26 '08 #2
Tony Johansson wrote:
If you do an update in the database you will use ExecuteNonQuery but
the database update will work just as good if you instead use ExecuteReader
or executeScalar.

So my question is how is this possible ?
So it seems to me that you can use whichever you like ?
Some database may send the SQL the same way no matter what it
is and just return null or garbage for the result set or value
not present. Other database may throw you an exception.

So it is both safest and most readable to use ExecuteNonQuery
for updates.

Arne
Aug 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark | last post by:
It appears that you can only retrieve an Output parameter from a SQL Server stored procedure when using the ExecuteNonQuery of the SqlCommand class, and cannot use the ExecuteReader() method. In...
1
by: Francis | last post by:
Hello, My question is: is it better to use DataSets than the simple ExecuteNonQuery() methods. I thought the DataSets would be a better structured way of programming, but it seems to result in...
2
by: Dinesh | last post by:
Hi, I have one stored procedure in SQL server in which i have written one insert statement. Now in my cs file i pass the parameters require to execute that stored procedure and finaly by mistaken...
7
by: Neven Klofutar | last post by:
Hi, I have a problem with SqlHelper.ExecuteScalar ... When I try to execute SqlHelper.ExecuteScalar I get this message: "System.InvalidCastException: Object must implement IConvertible.". ...
1
by: shahi | last post by:
Hi all, i have a sp that produce an error when execute by some special parameters. i write a program in vb.net that execute it, but i come accross a strange problem: when i execute the sp...
1
by: rn5a | last post by:
A class file has the following code: Imports System Imports System.Data Imports System.Data.SqlClient Namespace LoginUsers Public Class UserValidation Public Function Validate(ByVal...
2
by: Jason Huang | last post by:
Hi, I am not clear about the difference between the SqlCommand's ExecuteReader and ExecuteNonQuery. Would somebody show me an example to demonstrate the difference? Thanks for help. Jason
0
by: neoupadhyay | last post by:
ExecuteScalar: CommandText property has not been initialized public static bool GridView4_RowUpdate(string ItemDescription, decimal AnnualCost, Single CPIChildCost, decimal ChildCostItemTotal,...
2
by: Randy Smith | last post by:
Hi, I've got some weird behavior happening within one of the datamappers. It all has to do with inserting a new row, and returning the Id of the row being entered. Here is what the code...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.