2つの配列を持つ構造体を返す2つのメソッド1を持つWebサービスがあります。
public struct GetWeatherItemDataStruct
{
//public DateTime[] TimeStamp;
public double[] Value;
public string[] TimeStamp;
}
[WebMethod]
public GetWeatherItemDataStruct GetWeatherItemData(string parameterName,string fromTime,string toTime)
{
GetWeatherItemDataStruct gwiDataStructObj = new GetWeatherItemDataStruct();
SqlConnection conn = null;
SqlDataReader rdr = null;
int prameterID = GetParameterID(parameterName);
int tblSize = GetTableSize(parameterName, fromTime, toTime, prameterID);
double[] result = new double[tblSize];
int i = 0;
string[] tStamp = new string[tblSize];
String source = "Data Source=MASTER-PC\\SQLEXPRESS;Initial Catalog=WeatherDatabase;Integrated Security=True";
try
{
using (conn = new SqlConnection(source))// create and open a connection object
{
// 1. create a command object identifying
// the stored procedure
SqlCommand cmd = new SqlCommand("GetWeatherItemData", conn);
// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which
// will be passed to the stored procedure
//cmd.Parameters.Add(new SqlParameter("@WeatherParameterID", "1"));
cmd.Parameters.Add("@WeatherParameter", SqlDbType.VarChar).Value = parameterName;
cmd.Parameters.Add("@FromTimeStamp", SqlDbType.VarChar).Value = fromTime;
cmd.Parameters.Add("@ToTimeStamp", SqlDbType.VarChar).Value = toTime;
conn.Open();
// execute the command
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
result[i] = (double)rdr["MeasurementValue"];
tStamp[i] = rdr["Recieved"].ToString();
i++;
}
gwiDataStructObj.Value = result;
gwiDataStructObj.TimeStamp = tStamp;
int b = gwiDataStructObj.Value.Length;
}
}
catch (SqlException e)
{
//Log exception
//Display Error message
}
return gwiDataStructObj;
}
[WebMethod]
public int[] stringTest(string[] tString)
{
int numberOfStrings = tString.Length;
int[] returnS = new int[numberOfStrings];
for (int i = 0; i <= numberOfStrings; i++)
{
returnS[i] = 1;
}
return returnS;
}
クライアントプログラムでは、次のように構造体テーブルから読み取ることができます。
string dtFrom = "2012-10-04 19:05:57:190";
string dtTo = "2012-10-05 21:50:05:197";
double[] paramValue;
string[] timeStamp;
var client = new WebServiceSample.WebService1SoapClient();
paramValue = client.GetWeatherItemData(paramName, dtFrom, dtTo).Value.ToArray();
timeStamp = client.GetWeatherItemData(paramName, dtFrom, dtTo).TimeStamp.ToArray();
これはうまくいくようです。しかし、同じ構造体の配列を返す別のメソッドがあります。
public struct GetWeatherItemDataStruct
{
//public DateTime[] TimeStamp;
public double[] Value;
public string[] TimeStamp;
}
[WebMethod]
public GetWeatherItemDataStruct[] GetSelectedWeatherItemsData(string[] parameterName, string fromTime, string toTime)
{
int numbeOfParameters = parameterName.Length;
GetWeatherItemDataStruct[] getSelectedItemsStructObj = new GetWeatherItemDataStruct[numbeOfParameters];
SqlConnection conn = null;
SqlDataReader rdr = null;
int prameterID = 0;
int tblSize = 0;
double[] result;
int i = 0;
int counter = 0;
for (counter = 0; counter < numbeOfParameters; numbeOfParameters++)
{
prameterID = GetParameterID(parameterName[counter]);
tblSize = GetTableSize(parameterName[counter], fromTime, toTime, prameterID);
result = new double[tblSize];
string[] tStamp = new string[tblSize];
String source = "Data Source=MASTER-PC\\SQLEXPRESS;Initial Catalog=WeatherDatabase;Integrated Security=True";
try
{
using (conn = new SqlConnection(source))// create and open a connection object
{
// 1. create a command object identifying
// the stored procedure
SqlCommand cmd = new SqlCommand("GetWeatherItemData", conn);
// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which
// will be passed to the stored procedure
//cmd.Parameters.Add(new SqlParameter("@WeatherParameterID", "1"));
cmd.Parameters.Add("@WeatherParameter", SqlDbType.VarChar).Value = parameterName;
cmd.Parameters.Add("@FromTimeStamp", SqlDbType.VarChar).Value = fromTime;
cmd.Parameters.Add("@ToTimeStamp", SqlDbType.VarChar).Value = toTime;
conn.Open();
// execute the command
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
result[i] = (double)rdr["MeasurementValue"];
tStamp[i] = rdr["Recieved"].ToString();
i++;
}
getSelectedItemsStructObj[counter].Value = result;
getSelectedItemsStructObj[counter].TimeStamp = tStamp;
}
}
catch (SqlException e)
{
//Log exception
//Display Error message
}
}
return getSelectedItemsStructObj;
}
ここで私は立ち往生しています。各オブジェクトのテーブルを読み取るにはどうすればよいですか?