从WCF WebGet返回嵌套的复杂类型

时间:2012-02-15 19:30:57

标签: wcf types nested

我已经创建了一个wcf数据服务来返回复杂类型患者的List。我使用模型浏览器为“患者”创建复杂类型,为“联系人”创建复杂类型。我想将“Contacts”属性添加到“Patient”,这将是一个List。

如何将嵌套的复杂类型列表添加为属性并将其返回?

    [WebGet]
    public List<Patient> GetPatientByID(int tolid)
    {
        List<Patient> list = new List<Patient>();

        // create pds user session record
        SqlConnection conn = new SqlConnection("Data Source=server;Initial Catalog=db;User ID=<ID>;Password=<pwd>");
        SqlCommand cmd = new SqlCommand();

        try
        {
            // open the connection
            conn.Open();

            // configure the command
            cmd.Connection = conn;
            cmd.CommandText = "sp_tol_patient_select";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("tolid", tolid));

            // execute the command and convert the decimal value
            // returned by ExecuteScalar to an int to get new identity value
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Patient itm = new Patient();
                itm.MRN = reader["UNITNUMBER"].ToString();
                itm.AccountNum = reader["ACCTNUM"].ToString();
                itm.FullName = reader["PATNAME"].ToString();
                itm.BirthDate = reader["BIRTHDATE"].ToString();
                itm.Gender = reader["SEX"].ToString();

                list.Add(itm);
            }

            return list;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
        }
    }

0 个答案:

没有答案