-1

我对这段代码有一些严重的问题。我有一个方法GetWeatherItemData,它需要一个参数名称和一个句点作为从 SQL 数据库获取数据的输入,它似乎工作正常。

但是我想创建一个可以对多个参数执行相同工作的方法,所以我创建了一个名为的方法,该方法GetSelectedWeatherItemsData将参数数组作为输入并循环通过我的第一个方法,但由于某种原因,它只会返回 1 个参数的数据只有,输入参数数组中的第一个。

这是第一种方法:

public CustomDataType GetWeatherItemData(string parameterName, string fromTime, string toTime)
{
        /* This method takes parameter name, start time and end time as input it will then return 
         * all the measurement values and their timestamp as array for the specific parameter           
         */
        CustomDataType getWeatherItemObj = new CustomDataType();           
        List<double> valueList = new List<double>();
        List<string> timeStampList = new List<string>();
        List<int>   parameterIdList = new List<int>();
        List<string> ParameterNameList = new List<string>();

        try
        {
            using (conn = new SqlConnection(connectionString))// create and open a connection object
            {
                // 1. create a command object identifying the stored procedure
                 cmd = new SqlCommand("GetWeatherItemData", conn);

                // 2.Let the command object know we will execute a stored procedure
                cmd.CommandType = CommandType.StoredProcedure;

                // 3. add the 3 parameters to command, so the can be passed to the stored procedure                  
                cmd.Parameters.Add("@WeatherParameterName", SqlDbType.VarChar).Value    = parameterName;
                cmd.Parameters.Add("@FromTime", SqlDbType.VarChar).Value                = fromTime;
                cmd.Parameters.Add("@ToTime", SqlDbType.VarChar).Value                  = toTime;

                //open connection
                conn.Open();

                // execute the command
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        valueList.Add((double)reader["MeasurementValue"]);
                        timeStampList.Add(reader["MeasurementDateTime"].ToString());
                        parameterIdList.Add((int)reader["WeatherParameterID"]);                            
                    }
                } 

                //close connection
                reader.Close();

                //changed to arrays to support webservices
                getWeatherItemObj.arrayOfValue          = valueList.ToArray();
                getWeatherItemObj.arrayOfTimestamp      = timeStampList.ToArray();
                getWeatherItemObj.arrayOfParameterID    = parameterIdList.ToArray();                    

                for (counter = 0; counter < getWeatherItemObj.arrayOfValue.Length; counter++)
                {
                    ParameterNameList.Add(GetParameterInfo(parameterName).ParameterName);
                }

                getWeatherItemObj.arrayOfParameterName = ParameterNameList.ToArray();
            }
        }
        catch (SqlException e)
        {
            Console.WriteLine("Connection failed");
            Console.WriteLine(e.Message);
            Thread.Sleep(5000);
        }           
        return getWeatherItemObj;
    }

这是我有问题的代码。它采用参数名称数组和句点作为输入。但它只返回输入数组中第一个元素的数据,就好像它只执行一次 for 循环并跳出一样。我通过为它们分配一个固定数字来测试 for 循环内的代码,例如parameterName[3]而不是,parameterName[counter]这导致我获取该元素的数据而不是第一个元素。因此,出于某种原因,for 循环仅进行 1 次迭代。

public CustomDataType GetSelectedWeatherItemsData(string[] parameterName, string fromTime, string toTime)
{
        CustomDataType tempObj;     
        List<double> valueList          = new List<double>();
        List<string> timeStampList      = new List<string>();
        List<int> paramIdStampList      = new List<int>();
        List<string> ParameterNameList  = new List<string>();

        for (counter = 0; counter < (parameterName.Length); counter++)
        {
            tempObj = GetWeatherItemData(parameterName[counter], fromTime, toTime);
            valueList.AddRange(GetWeatherItemData(parameterName[counter], fromTime, toTime).arrayOfValue);
            timeStampList.AddRange(GetWeatherItemData(parameterName[counter], fromTime, toTime).arrayOfTimestamp);
            //paramIdStampList.AddRange(tempObj.arrayOfParameterID);
            ParameterNameList.AddRange(GetWeatherItemData(parameterName[counter], fromTime, toTime).arrayOfParameterName);
        }

        getSelectedItemsObj                         = new CustomDataType();
        getSelectedItemsObj.arrayOfValue            = valueList.ToArray();
        getSelectedItemsObj.arrayOfTimestamp        = timeStampList.ToArray();
        //getSelectedItemsObj.arrayOfParameterID    = paramIdStampList.ToArray();
        getSelectedItemsObj.arrayOfParameterName    = ParameterNameList.ToArray();

        return getSelectedItemsObj;
    }
4

1 回答 1

1

看起来绝对错误的事实是,您在循环中调用了简单的Web 服务四次- 为什么不只调用一次,然后使用返回的结果呢?

就像是:

for (counter = 0; counter < (parameterName.Length); counter++)
{
    tempObj = GetWeatherItemData(parameterName[counter], fromTime, toTime);

    valueList.AddRange(tempObj.arrayOfValue);
    timeStampList.AddRange(tempObj.arrayOfTimestamp);
    //paramIdStampList.AddRange(tempObj.arrayOfParameterID);
    ParameterNameList.AddRange(tempObj.arrayOfParameterName);
}
于 2012-11-19T06:45:11.033 回答