开发者

ObjectContext.ExecuteStoreCommand, how to clear parameters between calls?

I am making multiple calls to ObjectContext.ExecuteStoreCommand with different commands and different parameters, although I use the same parameter list (object) for a several of the commands. I am getting the following exception:

System.ArgumentException: The SqlParameter is already contained by another SqlParameterCollection.

Is there a way to clear parameters between calls as I would do with straight up ADO.NET?

Updated with a code sample:

    string sqlDeleteWebUserGreen = "delete WebUserGreen where WebUserId = @WebUserId";
    string sqlDeleteWebUserBlue = "delete WebUserBlue where WebUserId = @WebUserId";

    var argsDeleteWebUserXref = new DbParameter[] {
        new SqlParameter { ParameterName = "WebUserId", Value = user.WebUserId }

    rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserGreen, argsDeleteWebUserX开发者_JS百科ref);
    rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserBlue, argsDeleteWebUserXref);

UPDATE

Basically I am unable to find any better way of doing this, so I ended up accepting the answer below. The only difference is that I simply put the creation of the parameter into a separate method, so my calls look like base.context.ExecuteStoreCommand(sqlDeleteWebUserBlue, MethodThatWillGiveMeTheParameterArray());


The problem is that you are using the same parameter twice.

try this

var argsDeleteWebUserXref1 = new DbParameter[] {         new SqlParameter { ParameterName = "WebUserId", Value = user.WebUserId }  

var argsDeleteWebUserXref2 = new DbParameter[] {         new SqlParameter { ParameterName = "WebUserId", Value = user.WebUserId }  

rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserGreen, argsDeleteWebUserXref1);      
rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserBlue, argsDeleteWebUserXref2);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜