首页 > 解决方案 > 如何在 C# 中将查询结果作为字符串返回?

问题描述

下面的查询返回单个 VARCHAR 值,我想将此值作为字符串返回。我该怎么做?

代码片段:

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;
}

标签: c#mysql

解决方案


您可以创建一个列表并将值添加到列表中。返回列表后。


推荐阅读