我有这段代码,我得到错误,并且无法理解为什么我得到错误。代码如下:我使用两个数组一个字符串和一个双精度(数据值及其对应的tinestamp)。但由于某种原因,我收到错误:这一行的索引超出范围错误:
getSelectedItemsObj.arrayOfTimeStamp = GetItemData(parameterName[counter], fromTime, toTime).arrayOfTimeStamp;
如果我删除这一行并且只使用 arrayOfValue 代码工作正常,但我需要两者
.......... 感谢您的快速回复,这是 GetItemData 的设置方式
public CustomDataType GetItemData(string parameterName, string fromTime, string toTime)
{
getWeatherItemObj = new CustomDataType();
// get the parameter ID
prameterID = GetParameterInfo(parameterName).ParameterID;
//get the nr of items to size value arrays
tableSize = GetTableSize(parameterName, fromTime, toTime, prameterID);
getWeatherItemObj.arrayOfValue = new double[tableSize];
getWeatherItemObj.arrayOfTimeStamp = new string[tableSize];
counter = 0;
try
{
using (conn = new SqlConnection(connectionString))// create and open a connection object
{
// 1. create a command object identifying the stored procedure
cmd = new SqlCommand("GetItemData", 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("@ParameterName", 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())
{
getWeatherItemObj.arrayOfValue[counter] = (double)reader["MeasurementValue"];
getWeatherItemObj.arrayOfTimeStamp[counter] = reader["MeasurementDateTime"].ToString();
counter++;
}
}
//close connection
reader.Close();
}
}
catch (SqlException e)
{
Console.WriteLine("Connection failed");
Console.WriteLine(e.Message);
Thread.Sleep(5000);
}
return getWeatherItemObj;
}
class CustomDataType
{
public double[] arrayOfValue;
public string[] arrayOfTimeStamp;
}
public CustomDataType GetSelectedtemsData(string[] parameterName, string fromTime, string toTime)
{
numberOfParameters = parameterName.Length;//Get the number of given parameters
tableSize = 0;
for (counter = 0; counter < numberOfParameters; counter++)
{
tableSize = tableSize + GetItemData(parameterName[counter], fromTime, toTime).arrayOfTimeStamp.Length;
}
getSelectedItemsObj = new CustomDataType();
getSelectedItemsObj.arrayOfValue = new double[tableSize];
getSelectedItemsObj.arrayOfTimeStamp = new string[tableSize];
for (counter = 0; counter < tableSize; counter++)
{
getSelectedItemsObj.arrayOfValue = GetItemData(parameterName[counter], fromTime, toTime).arrayOfValue;
getSelectedItemsObj.arrayOfTimeStamp = GetItemData(parameterName[counter], fromTime, toTime).arrayOfTimeStamp;
}
return getSelectedItemsObj;
}