开发者

Is it possible to assign the result of an SQL to a variable?

开发者 https://www.devze.com 2023-01-28 06:33 出处:网络
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.

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

精彩评论

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