从Webservice返回多行

时间:2016-01-15 01:57:41

标签: asp.net web-services

我有一个网络服务,它没有错误,但只给我一行数据

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;


namespace nikitaweb
{
    /// <summary>
    /// Summary description for nikitaws
    /// </summary>
    [WebService(Namespace = "http://ijportal.kemenkeu.go.id/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class nikitaws : System.Web.Services.WebService
    {


        public class Stndlist
        {
            public string nond { get; set; }
            public string nost { get; set; }
            public string tglst { get; set; }
            public string halst { get; set; }
        }

    [WebMethod]
    public Stndlist GetStnd()
    {
        string CS = ConfigurationManager.ConnectionStrings["nikita_cs"].ConnectionString;
        List<Stndlist> Stndlists = new List<Stndlist>();
        using (SqlConnection con = new SqlConnection(CS))
        {
            Stndlist daftarstnd = new Stndlist();
            SqlCommand da = new SqlCommand("xp_TampilStNd", con);
            da.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader rdr = da.ExecuteReader();
            while (rdr.Read())
            {
                daftarstnd.nond = rdr["nond"].ToString();
                daftarstnd.nost = rdr["nost"].ToString();
                daftarstnd.tglst = rdr["tglst"].ToString();
                daftarstnd.halst = rdr["halst"].ToString();
                Stndlists.Add(daftarstnd);
            }

            //JavaScriptSerializer js = new JavaScriptSerializer();
            //Context.Response.Write(js.Serialize(daftarstnd));
            return daftarstnd;

        }
    }

}

}

当我运行网络服务时,只给我一行

<Stndlist>
<nond>ND-(002)/PP.42/2013</nond>
<nost>ST-(005)/PP/2013</nost>
<tglst>11/26/2013 12:00:00 AM</tglst>
<halst>Usulan Tugas Benchmarking ke Bandung</halst>
</Stndlist>

我的目标是返回我的sql server数据库中的所有行。 你能告诉我怎么修改这段代码? 谢谢。

1 个答案:

答案 0 :(得分:2)

您需要从Web服务返回一组对象。在您的情况下,最方便使用的集合是List<Stndlist>。使用以下代码作为Web服务方法。 Web服务将自动在幕后将集合对象转换为序列化的JSON。

[WebMethod]
public List<Stndlist> GetStnd()
    {
        string CS = ConfigurationManager.ConnectionStrings["nikita_cs"].ConnectionString;

        Stndlist daftarstnd = null;
        List<Stndlist> Stndlists = new List<Stndlist>();

        using (SqlConnection con = new SqlConnection(CS))
        {

            SqlCommand da = new SqlCommand("xp_TampilStNd", con);
            da.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader rdr = da.ExecuteReader();
            while (rdr.Read())
            {
                daftarstnd = new Stndlist();
                daftarstnd.nond = rdr["nond"].ToString();
                daftarstnd.nost = rdr["nost"].ToString();
                daftarstnd.tglst = rdr["tglst"].ToString();
                daftarstnd.halst = rdr["halst"].ToString();
                Stndlists.Add(daftarstnd);
            }
       }
     return Stndlists;
  }