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.