解决不了"的SqlParameter已经被其他SqlparameterCollection"载;

解决不了"的SqlParameter已经被其他SqlparameterCollection"载;

问题描述:

我在一个窗口服务使用2个线程(来自同一个类)。我总是得到同样的错误消息:

I am using 2 threads (from same class) in a windows service. I always getting the same error message:

"The SqlParameter is already contained by another SqlParameterCollection.
   at System.Data.SqlClient.SqlParameterCollection.Validate(Int32 index, Object value)
   at System.Data.SqlClient.SqlParameterCollection.Add(Object value)
   at System.Data.SqlClient.SqlParameterCollection.Add(SqlParameter value)
   at DataBaseLayer.SqlDataBaseLayer.FillDataSetFromProcedure(String strStoredProc, ArrayList parameterCollection)
   at TestThread.StartThreads()"

我试图通过创建SqlParameters和一个ArrayList的新实例来解决问题。我也试过在我的清除的ArrayList在代码中循环。这没有解决的问题。我愿意接受任何建议。

I tried to solve the problem by creating new instances of SqlParameters and an Arraylist. I also tried to clear the arraylists at the and of my for loop in the code. This does not solve the problem. I am open to any advice.

您尝试添加一个的SqlParameter SqlParameterCollection 的两倍。这可能会或可能不会在线程间发生的事情。

You are trying to add a SqlParameter to a SqlParameterCollection twice. This may or may not be happening across threads.

如果这是一个多线程问题,那么所有的变量应该在本地范围,因为,如果他们不是你应该在thier访问implemeting sychronisation,可能与锁定

If this is a multi-threading issue then all your variables should be scoped locally because, if they are not you should be implemeting sychronisation on thier access, probably with lock.

如果这不是并发问题的问题应该很容易做一个确定的查找所有引用的SqlParameter 变量。

If this is not a concurrency problem the issue should easily be identified by doing a Find All References on your SqlParameter variable.

无论哪种方式,我们可以更帮助你,如果您发布一些代码。

Either way we can help you more if you post some code.