.net web services,返回1条以上记录

时间:2012-05-12 03:01:51

标签: .net web-services ksoap2

我正在开发一个.net webservices,但结果总是只返回第一条记录,我如何修复while循环以便返回多于1?下面是示例代码: 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;
        }
    }
}

1 个答案:

答案 0 :(得分: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 userType=@userType", 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;
}