What are the different cases when we use these three? Where should I use one and where should I not?
ExecuteScalar
is typically used when your query returns a single value. If it returns more, then the result is the first column of the first row. An example might be SELECT @@IDENTITY AS 'Identity'
.ExecuteReader
is used for any result set with multiple rows/columns (e.g., SELECT col1, col2 from sometable
).ExecuteNonQuery
is typically used for SQL statements without results (e.g., UPDATE, INSERT, etc.).ExecuteNonQuery():
ExecuteReader():
ExecuteScalar():
Reference URL:
http://nareshkamuni.blogspot.in/2012/05/what-is-difference-between.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With