.net Webサービスを開発していますが、結果は常に最初のレコードのみを返します。whileループを修正して、複数を返すことができるようにするにはどうすればよいですか?以下はサンプルコードです:DAL.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 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;
}
}
}