需要WebMethod返回整个查询内容,而不是第一项
本文关键字:一项 返回 WebMethod 查询 需要 | 更新日期: 2023-09-27 18:19:53
我有一个可以工作的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;
}
看起来您正在用查询返回的所有行更新同一个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;
}
}
其他选项:在返回语句中使用DTO_getList作为数组:
[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();
}
}