Can't solve “Sqlparameter is already contained by another SqlparameterCollection”

跟風遠走 提交于 2019-12-04 06:09:40

问题


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()"

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.


回答1:


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

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.

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.




回答2:


Dont share the same sql objects between 2 thread. Use the pool collection by SQL



来源:https://stackoverflow.com/questions/6289607/cant-solve-sqlparameter-is-already-contained-by-another-sqlparametercollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!