返回struct对象的Webservice方法

时间:2012-10-20 22:28:00

标签: c# asp.net .net web-services soap

我有一个带有两个方法1的webservice,它返回一个带有两个数组的结构:

 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;
}

这里我卡住了。我如何阅读每个对象的表格?

1 个答案:

答案 0 :(得分:0)

基于上述评论。

要从数组中的结构中获取数据,您可以像设置它一样检索它。

myArrayOfStruct[0].myStructProperty

如果它是结构中的数组,那就是:

myArrayOfStruct[0].myArrayProperty[0]

如果你想循环遍历数组中的所有结构和数组属性中的所有项,你接下来会有两个循环:

for (int i = 0; i < myArrayOfStruct.length; i++){
   for (int j = 0; j < myArrayProperty.length; j++){
      myData = myArrayOfStruct[i].myArrayProperty[j]; 
      // do something with the data
   }
}

基本上,myArrayOfStruct[0].myProperty与访问它是一样的:

MyStruct myStruct = myArrayOfStruct[0];
myStruct.myProperty

这是否回答了你的问题?你在上面正确设置它。您以类似的方式检索它。