C# 为什么对存储过程的泛型调用会在加载时引发异常?过程不是线程安全的吗?

C# 为什么对存储过程的泛型调用会在加载时引发异常?过程不是线程安全的吗?,c#,stored-procedures,C#,Stored Procedures,更新 一旦我查看变量ii,发现它是一个类变量,我就能够将它移动到方法中进行定义,所有这些都正常工作。我95%相信这是由于多个线程同时更新ii。有人能为我解释一下这种行为吗 我的库方法已经运行了几年,但最近抛出了一个系统。IndexOutoforangeException,我一辈子都不知道为什么。我已经在catch块的中设置了一个断点,并计算出ii被设置为5,但它永远不会达到5。这些参数是一个返回代码,具有返回值的方向。下一个和最后3个具有输出方向。spParams有3个参数。我能想到的唯一一件事

更新 一旦我查看变量ii,发现它是一个类变量,我就能够将它移动到方法中进行定义,所有这些都正常工作。我95%相信这是由于多个线程同时更新ii。有人能为我解释一下这种行为吗

我的库方法已经运行了几年,但最近抛出了一个系统。IndexOutoforangeException,我一辈子都不知道为什么。我已经在catch块的中设置了一个断点,并计算出ii被设置为5,但它永远不会达到5。这些参数是一个返回代码,具有返回值的方向。下一个和最后3个具有输出方向。spParams有3个参数。我能想到的唯一一件事是它与线程有关,但它确实需要一些帮助来调试这里发生的事情。如果我也重新发送它,它对同一个呼叫有效

    public DataSet GetDataSetFromStoredProcWithObjectParams(string spName, object[] spParams)
    {
        // First Create a New Connection
        using (var sqlConn = new SqlConnection())
        {
            sqlConn.ConnectionString = ConnectionString;
            sqlConn.Open();

            var cmd = new SqlCommand()
            {
                CommandType = CommandType.StoredProcedure,
                CommandText = spName,
                Connection = sqlConn,
                CommandTimeout = iSqlCommandTimout
            };

            SqlCommandBuilder.DeriveParameters(cmd);

            // Assumption here is that sp param list order matches object list order.
            try
            {
                ii = 0;
                for (var idx = 0; idx < cmd.Parameters.Count; idx++)
                {
                    if (cmd.Parameters[idx].Direction != ParameterDirection.ReturnValue && (cmd.Parameters[idx].Direction == ParameterDirection.Input || cmd.Parameters[idx].Direction == ParameterDirection.InputOutput))
                        cmd.Parameters[idx].Value = spParams[ii++];
                }
            } catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Stored Procedure being processes is: " + spName);
                for (var j = 0; j < spParams.Length; j++)
                {
                    System.Diagnostics.Debug.WriteLine("Stored procedure Parament " + j + " is " + spName[j]);
                }
                log.Error(ex);
                throw (ex);
            }

            // Create an Adapter
            var da = new SqlDataAdapter(cmd);
            var ds = new DataSet();

            // Fill The DataSet With the Contents of the Stock Table
            da.Fill(ds);
            return (ds.Tables[0].Rows.Count == 0) ? null : ds;
        }
    }
公共数据集GetDataSetFromStoredProcWithObjectParams(字符串spName,对象[]spParams)
{
//首先创建一个新连接
使用(var sqlConn=new SqlConnection())
{
sqlConn.ConnectionString=ConnectionString;
sqlConn.Open();
var cmd=new SqlCommand()
{
CommandType=CommandType.StoredProcess,
CommandText=spName,
连接=sqlConn,
CommandTimeout=iSqlCommandTimout
};
SqlCommandBuilder.DeriveParameters(cmd);
//这里的假设是sp param列表顺序与对象列表顺序匹配。
尝试
{
ii=0;
对于(var idx=0;idx
ii
字段吗?为什么不是本地的?无论如何,在调试器中单步执行或对所有内容进行跟踪。找出发生了什么事。如果涉及到线程,跟踪将比调试器干扰更小。在
catch
块中转储
cmd.Parameters
,至少是名称和方向。它显然不符合
spParams
,但您确定知道哪个是错误的吗?