C# 需要WebMethod返回整个查询内容,而不是第一项

C# 需要WebMethod返回整个查询内容,而不是第一项,c#,asp.net,.net,asmx,webmethod,C#,Asp.net,.net,Asmx,Webmethod,我有一个web方法可以工作,但是我输入的原始DB查询返回一个项目列表,而web方法只返回第一个项目。我需要它返回整个列表,而不仅仅是第一项。我该怎么做 [WebMethod] public DTO_getList getList(int partID, int offset, int next) { DTO_getList user = new DTO_getList(); SqlConnection conn =

我有一个web方法可以工作,但是我输入的原始DB查询返回一个项目列表,而web方法只返回第一个项目。我需要它返回整个列表,而不仅仅是第一项。我该怎么做

[WebMethod]
        public DTO_getList getList(int partID, int offset, int next)
        {
            DTO_getList user = new DTO_getList();

            SqlConnection conn = new SqlConnection();
            try
            {
                conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MYCONNECTION"].ConnectionString;
                conn.Open();
                SqlDataReader myReader = null;
                string sqlQuery = "LENGTHY QUERY THAT RETURNS A LIST OF STUFF";

                SqlCommand myCommand = new SqlCommand(sqlQuery, conn);
                //  Add the parameters to be passed into the DB
                myCommand.Parameters.AddWithValue("@partID", partID);
                myCommand.Parameters.AddWithValue("@offset", offset);
                myCommand.Parameters.AddWithValue("@next", next);
                myReader = myCommand.ExecuteReader();

                while (myReader.Read())
                {
                    //  Grab the records
            user.date = myReader["Date"].ToString();
            //MORE STUFF BEING RETURNED

                }
            }
            catch (Exception Ex)
            {
                //user.valid = false;

                return user;
            }
            finally
            {
                conn.Close();
            }
                return user;
}

看起来您正在使用查询返回的所有行更新同一个用户实例,并返回该实例

而是创建一个用户列表System.Collections.Generic.List,并为每一行添加一个新的Uer实例:

myUsersList = new List<User>();

while (myReader.Read())
{
    myUsersList.Add(new User()
    {
        data = myReader["Date"].ToString(),
        ........
    }
}

return myUsersList.ToArray();
创建类用户

使用这个代码

[WebMethod]
public List<User>  getList(int partID, int offset, int next)
    {

        List<User> userList = new List<User>();


        SqlConnection conn = new SqlConnection();
        try
        {
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MYCONNECTION"].ConnectionString;
            conn.Open();
            SqlDataReader myReader = null;
            string sqlQuery = "LENGTHY QUERY THAT RETURNS A LIST OF STUFF";

            SqlCommand myCommand = new SqlCommand(sqlQuery, conn);
            //  Add the parameters to be passed into the DB
            myCommand.Parameters.AddWithValue("@partID", partID);
            myCommand.Parameters.AddWithValue("@offset", offset);
            myCommand.Parameters.AddWithValue("@next", next);
            myReader = myCommand.ExecuteReader();

            User us = new User();
            while (myReader.Read())
            {
                //  Grab the records
                us.Date = myReader["Date"].ToString();
                //MORE STUFF BEING RETURNED
                userList.Add(us);
            }
        }
        catch (Exception Ex)
        {
            //user.valid = false;

            return userList;
        }
        finally
        {
            conn.Close();
        }
        return userList;
    }

在返回语句中使用DTO_getList作为列表:

   [WebMethod]
    public List<DTO_getList> getList(int partID, int offset, int next)
    {
        List<DTO_getList> list = new List<DTO_getList>();
        SqlConnection conn = new SqlConnection();
        try
        {
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MYCONNECTION"].ConnectionString;
            conn.Open();
            SqlDataReader myReader = null;
            string sqlQuery = "LENGTHY QUERY THAT RETURNS A LIST OF STUFF";

            SqlCommand myCommand = new SqlCommand(sqlQuery, conn);
            //  Add the parameters to be passed into the DB
            myCommand.Parameters.AddWithValue("@partID", partID);
            myCommand.Parameters.AddWithValue("@offset", offset);
            myCommand.Parameters.AddWithValue("@next", next);
            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                 DTO_getList user = new DTO_getList();
                //  Grab the records
                user.date = myReader["Date"].ToString();
                //MORE STUFF BEING RETURNED
                list.Add(user);
            }

        }
        catch (Exception Ex)
        {
            //user.valid = false;
            return list;
        }
        finally
        {
            conn.Close();
        }

            return list;
       }
    }
    [WebMethod]
    public DTO_getList[] getList(int partID, int offset, int next)
    {
       List<DTO_getList> list = new List<DTO_getList>();
        SqlConnection conn = new SqlConnection();
        try
        {
          //Same repetitive code here
        }
        catch (Exception Ex)
        {
            //user.valid = false;
            return list.ToArray();
        }
        finally
        {
            conn.Close();
        }

            return list.ToArray();
       }
    }
其他选项:在return语句中使用DTO_getList作为数组:

   [WebMethod]
    public List<DTO_getList> getList(int partID, int offset, int next)
    {
        List<DTO_getList> list = new List<DTO_getList>();
        SqlConnection conn = new SqlConnection();
        try
        {
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MYCONNECTION"].ConnectionString;
            conn.Open();
            SqlDataReader myReader = null;
            string sqlQuery = "LENGTHY QUERY THAT RETURNS A LIST OF STUFF";

            SqlCommand myCommand = new SqlCommand(sqlQuery, conn);
            //  Add the parameters to be passed into the DB
            myCommand.Parameters.AddWithValue("@partID", partID);
            myCommand.Parameters.AddWithValue("@offset", offset);
            myCommand.Parameters.AddWithValue("@next", next);
            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                 DTO_getList user = new DTO_getList();
                //  Grab the records
                user.date = myReader["Date"].ToString();
                //MORE STUFF BEING RETURNED
                list.Add(user);
            }

        }
        catch (Exception Ex)
        {
            //user.valid = false;
            return list;
        }
        finally
        {
            conn.Close();
        }

            return list;
       }
    }
    [WebMethod]
    public DTO_getList[] getList(int partID, int offset, int next)
    {
       List<DTO_getList> list = new List<DTO_getList>();
        SqlConnection conn = new SqlConnection();
        try
        {
          //Same repetitive code here
        }
        catch (Exception Ex)
        {
            //user.valid = false;
            return list.ToArray();
        }
        finally
        {
            conn.Close();
        }

            return list.ToArray();
       }
    }

这不会返回列表,尽管代码没有中断。它回来了,嗯。它也只是简单地返回