executeReader and executeNonQuery

  • Thread starter Thread starter Benny Chow
  • Start date Start date
B

Benny Chow

Hello all,

I have a sql string that including both insert and select, for example:

sql = " INSERT INTO table1 (id, name) VALUES ('100', 'hello world') ";
sql += " SELECT MAX(id) FROM table1 ";

I want to execute these two sql statement in one execution, so i decided
to use executeReader. However, a friend told me that he read an article
says that it's not a good idea to exeute non-query (insert) with the
executeReader, but can't remember what are the disadvantages.

Is there anyone in here can give me a hand telling me why?


Thanks,

Benny
 
Benny,

If you weren't returning any data, then there's the overhead of creating an
empty reader. In your case you are returning data, but to use the
ExecuteReader method to return a single value is not really the best way to
go, because there's still an overhead. Why not use ExecuteScalar instead.
 
So if i use executeReader/executeScalar for a sql statement that
including INSERT and SELECT, what are the disadvantages?

For example:

sql = " INSERT INTO table1 (id, name) VALUES ('100', 'hello world') ";
sql += " SELECT MAX(id) FROM table1 ";


Thanks,

Benny
 
See my article in SS Mag http://www.betav.com/sql_server_magazine.htm (Fast
Answers) for a discussion of the differences.
hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top