简体   繁体   中英

How to wait for a database command to complete one iteration before continuing in foreach loop

I would like to know how to wait for a database command to complete before going to the next iteration in a for each loop.

The following code send all the SQL statement "EXEC ups_StoredProc" almost together without waiting for the next one to complete:

foreach (var guid in anUserIDList)
            {
                Database.ExecuteCmd(string.Format("EXEC dbo.usp_StoredProc @MemberContactID = '{0}'", guid), con);
            }

I've tried to get the result of the stored proc in a variable but it returns 0 even though the command did not complete yet.

int result =  Database.ExecuteCmd(string.Format("EXEC dbo.usp_StoredProc @MemberContactID = '{0}'", guid), con);

Thank you

You'd have to check on the documentation for that call.

From your comment, I see the method signature is:

public static int ExecuteCmd(string SQLCommand, IDbConnection con, params DBParam[] Params);

From my experience, I would hazard the guess that this is already a blocking call, and that it returns the number of affected rows. If that's the case, what you've got should work.

What makes you think the stored procedure should be returning anything other than 0 ? Are you sure that's working?

If, as you've suggested, this is a non-blocking call, there's probably nothing you can do. Unless that returned integer refers to some joinable task, it's probably "lost." That would be an awful design decision, though, so I really do doubt it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM