[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

good practise of using command.ExecuteNonQuery()

Posted on 2004-11-15
8
Medium Priority
?
301 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:csetzkorn
  • 4
  • 3
8 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12582270
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
 

Author Comment

by:csetzkorn
ID: 12582286
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12582317
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:csetzkorn
ID: 12582358
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
 
LVL 4

Expert Comment

by:vinhthuy_nguyen
ID: 12582377
Hi,
Did you mean that you want to use something like this :

Try
'ExecuteNonQuery1
'ExecuteNonQuery2
Catch e as Exception

Finally
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12582378
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
 

Author Comment

by:csetzkorn
ID: 12582400
I am currently using a reader twice (I close it before I use it again).
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 375 total points
ID: 12582413
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 13 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question