简体   繁体   English

在继续foreach循环之前,如何等待数据库命令完成一次迭代

[英]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: 以下代码几乎将所有SQL语句“ EXEC ups_StoredProc”一起发送,而无需等待下一个语句完成:

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. 我试图获取存储在变量中的proc的结果,但是即使命令尚未完成,它也会返回0。

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 ? 是什么让您认为存储过程应该返回0以外的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. 但是,那将是一个糟糕的设计决定,所以我确实对此表示怀疑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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