good practise of using command.ExecuteNonQuery()

Dear all,

I am just wondering whether it is a good practise to use several command.ExecuteNonQuery() invocations (for example) in one try/catch block?

Many thanks,

Christian
csetzkornAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
Hi csetzkorn,

the use of ExecuteNonQuery is appropriate when you don't need to return anything from database, but just to execute the code and eventually to know how many records have been afexted. This method is commonly used when you delete records, or update records but you don't need to return the new information or some parameters because the ExecuteNonQuery will not gives you this information

Regards!
B..M
mmarinov
0
csetzkornAuthor Commented:
Ok, I didn't nec. mean ExecuteNonQuery but also several ExecuteScalar(). Is it still ok to use several of these statements in one try/catch block?
0
mmarinovCommented:
csetzkorn,

it is not a problem. actually if i have such a situation i try to optimize the functionality and do all of the things within one call to the database. But if you don't want or you can not do it - go with several.
The one problem that you can receive is when an error arise it will be hard to debug if your calls are near to execution results and you will have to step-by-step to understand where the problem arises

Regards!
B..M
mmarinov
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

csetzkornAuthor Commented:
Yeah, thanks I thought about this. The problem is that I sometimes obtain a:

Commands out of sync; You can't run this command now

However this seems to be realeted to datareaders.

Thanks.

Chris
0
vinhthuy_nguyenCommented:
Hi,
Did you mean that you want to use something like this :

Try
'ExecuteNonQuery1
'ExecuteNonQuery2
Catch e as Exception

Finally
0
mmarinovCommented:
csetzkorn,

if you use datareader i don't think you can work with 2 datareader within one connection
for the ExecuteScalar() may be ( i haven't gone through this ) you have to open/close the connection - which can slow down the performance

Regards!
B..M
mmarinov
0
csetzkornAuthor Commented:
I am currently using a reader twice (I close it before I use it again).
0
mmarinovCommented:
csetzkorn,

i suggest you ( if it possible ) to do all the sql statements within only one call because the open/close scenario if slowing down your application and it is not so common

Regards!
B..M
mmarinov
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.