我试图从C#中的一个表中拉出总共24行,但它只拉出最后一行。
static class RoomObjects
{
public static string buildObjects(int roomid)
{
string pList = "";
MySqlDataReader mysqlRead = DBManager.database.getCommand("SELECT * FROM `objects` WHERE `roomid` = " + roomid + "").ExecuteReader();
while(mysqlRead.Read())
{
pList = mysqlRead["id"].ToString() + (char)8 + mysqlRead["sprite"].ToString() + (char)8 + mysqlRead["x"].ToString() + (char)8 + mysqlRead["y"].ToString() + (char)8 + mysqlRead["z"] + (char)8 + mysqlRead["rotation"] + (char)8;
}
DBManager.database.closeClient();
return pList;
}
我已经看到了许多使用此技术的示例,是否错过了某些内容,还是需要循环?
最佳答案
每次分配给pList
时,都将覆盖以前的内容。
如果要将结果存储为包含每个值的字符串,则需要将其附加到字符串中,而不是对其进行赋值。像这样:
static class RoomObjects
{
public static string buildObjects(int roomid)
{
string pList = "";
MySqlDataReader mysqlRead = DBManager.database.getCommand("SELECT * FROM `objects` WHERE `roomid` = " + roomid + "").ExecuteReader();
while(mysqlRead.Read())
{
pList += mysqlRead["id"].ToString() + (char)8 + mysqlRead["sprite"].ToString() + (char)8 + mysqlRead["x"].ToString() + (char)8 + mysqlRead["y"].ToString() + (char)8 + mysqlRead["z"] + (char)8 + mysqlRead["rotation"] + (char)8;
pList += "," // to separate the values.
}
DBManager.database.closeClient();
return pList;
}
关于c# - 搜索全部只给出最后一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35021497/