我有一个可行的Web方法,但是我输入的原始数据库查询会返回一个项目列表,而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;
}
答案 0 :(得分:0)
看起来您正在使用查询返回的所有行更新同一个User实例并返回该实例。
而是创建一个用户列表(System.Collections.Generic.List),并为每一行添加一个新的Uer实例:
myUsersList = new List<User>();
while (myReader.Read())
{
myUsersList.Add(new User()
{
data = myReader["Date"].ToString(),
........
}
}
return myUsersList.ToArray();
答案 1 :(得分:0)
创建课程“用户”
并使用此代码
[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;
}
答案 2 :(得分:0)
在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;
}
}
其他选项:在return语句中使用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();
}
}