2016-01-15 70 views
0

返回多個行我有一個Web服務,它不錯誤,但給我的數據只有一排從web服務

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; 

     } 
    } 

} 

}

當我運行web服務只給我一個行

<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服務器數據庫中的所有行。 你能告訴我如何修改這段代碼? 謝謝。

回答

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

你有兩個同名的變量。 – mason

+0

@mason,謝謝你指出。 – Sunil