簡體   English   中英

如何在 C# 中將查詢結果作為字符串返回?

[英]How to return a query result as a string in 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;
}

您可以創建一個列表並將值添加到列表中。 返回列表后。

如何將 VARCHAR 查詢結果作為字符串返回:

while (reader.Read())
{
        results= reader.GetString(0);                          // Added this
}
return results;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM