Is it possible to assign the result of an SQL to a variable?
Can I assign the r开发者_运维技巧esult of an SQL select command to a local variable in ASP.NET?
SqlCommand.ExecuteScalar is what you are after.
Example:
var command = new SqlCommand("SELECT TOP 1 age FROM example_table");
var ageValue = command.ExecuteScalar() as int?;
It sounds like you are looking for ExecuteScalar.
Scalar result: sure, just use ExecuteScalar.
Non-scalar: use a DataSet, or do some processing to build a collection of items containing the data you want
精彩评论