[英]How to return a query result as a string in C#?
The query below returns a single VARCHAR value and I want to return this value as a string.下面的查询返回单个 VARCHAR 值,我想将此值作为字符串返回。 How would I do that?我该怎么做?
string query = "SELECT " + FieldName + " FROM " + Table4 + " WHERE StartTime=" +
StartTime + " AND SystemId=" + SystemId + '"';
string result = "";
MySqlCommand cmd = new MySqlCommand(query, connection);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
result = ... // set "result" to the output of the query here
}
return result;
}
You can create a List and add the values to the list.您可以创建一个列表并将值添加到列表中。 After return the List.返回列表后。
How to return a VARCHAR query result as a string:如何将 VARCHAR 查询结果作为字符串返回:
while (reader.Read())
{
results= reader.GetString(0); // Added this
}
return results;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.