简体   繁体   中英

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. How would I do that?

Code snippet:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM