开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜