2012-05-12 74 views
0

我正在開發.net webservices,但結果總是隻返回第一條記錄,我該如何修復while循環,以便它可以返回超過1?下面是示例代碼: DAL.cs.net webservices,返回超過1條記錄

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
using System.Collections.Generic; 

namespace Data 
{ 
    public class DAL 
    { 
     public static Model.customer GetCustomer(string custID) 
     { 
      string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
      SqlConnection conn = null; 
      SqlDataReader reader = null; 
      try 
      { 
       conn = new SqlConnection(cs); 
       string sql = "SELECT * FROM member WHERE userType = '" + custID + "'"; 
       SqlCommand cmd = new SqlCommand(sql, conn); 
       conn.Open(); 
       reader = cmd.ExecuteReader(); 

       List<Model.customer> list = new List<Model.customer>(); 

       while (reader.Read()) 
       { 
        Model.customer cust = new Model.customer(); 
        cust.customerID = reader["fullName"].ToString(); 
        cust.contactName = reader["handphone"].ToString(); 
        cust.companyName = reader["NRIC"].ToString(); 
        list.Add(cust); 
       } 
       //error 
       return list; 
       //end of error 
      } 
      catch (Exception e) 
      { 
       HttpContext.Current.Trace.Warn("Error", "error in getcustomer()", e); 
      } 

      finally 
      { 
       if (reader != null) reader.Close(); 
       if (conn != null && conn.State != ConnectionState.Closed) conn.Close(); 
      } 
      return null; 
     } 
    } 
} 

customer.cs

using System; 

namespace Model 
{   
    public class customer 
    { 
     private string _customerID; 
     private string _companyName; 
     private string _contactName; 

     public string customerID 
     { 
      get { return _customerID; } 
      set { _customerID = value; } 
     } 

     public string companyName 
     { 
      get { return _companyName; } 
      set { _companyName = value; } 
     } 

     public string contactName 
     { 
      get { return _contactName; } 
      set { _contactName = value; } 
     } 
    } 
} 

service.cs

using System; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Collections.Generic; 

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class Service : System.Web.Services.WebService 
{ 
    public Service() { 

     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 

    [WebMethod] 
    //public Model.customer GetCustomer(string custID)   
    public static List<Model.customer> GetCustomer(string custID) 
    {   
     return Biz.BAL.GetCustomer(custID); 
    } 
} 

BAL.cs

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
using System.Collections.Generic; 

namespace Biz 
{ 
    public class BAL 
    { 
     public static List<Model.customer> GetCustomer(string custID) 
     { 
      Model.customer cust = Data.DAL.GetCustomer(custID); 
      cust.companyName = cust.companyName; 
      List<Model.customer> myList = new List<Model.customer>(); 

      myList.Add(cust); 

      return myList; 
     } 
    } 
} 
+0

這是WCF服務還是ASMX服務?如果沒有其他選擇,您應該只使用ASMX。 –

+0

這是asmx :) – melvintcs

回答

1

你必須返回List<T>,而不是單一實例,以便改變你的WebMethod的返回類型List<Model.customer>.

編輯:

使用參數/預編譯SQL語句或存儲過程,以避免SQL注入攻擊。 (即,從不使用硬編碼的SQL字符串。)

始終使用using語句正確地處置disposable object(s)

public static List<Model.customer> GetCustomers(String type) 
{ 
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
List<Model.customer> list=new List<Model.customer>(); 
using (SqlConnection cn = new SqlConnection(cs)) 
{ 
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM member WHERE [email protected]", cn)) 
    { 
     cmd.Parameters.Add("@userType",System.Data.SqlDbType.VarChar,20).Value=type; 
     cn.Open(); 
     using (SqlDataReader reader = cmd.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 
       Model.customer cust = new Model.customer(); 
       cust.fullName = reader["fullName"].ToString(); 
       cust.handPhone = reader["handphone"].ToString(); 
       cust.NRIC = reader["NRIC"].ToString(); 
       list.Add(cust); 
      } 
     } 
    } 
} 
return list; 
} 
+1

+1。如果你想擴大你的答案,包括正確使用「使用」塊,對我來說很方便。這樣,我就不必發佈一個單獨的CW答案。 –

+0

列表是缺少的命名空間,我應該怎麼做? – melvintcs

+0

@melvintcs您需要導入System.Collections.Generic命名空間。 – adatapost