Tag: tryparse executioncalar

Int32.TryParse()或(int?)command.ExecuteScalar()

我有一个SQL查询,它只返回一个字段 – 一个INT类型的ID。 我必须在C#代码中将其用作整数。 哪种方式更快,占用内存更少? int id; if(Int32.TryParse(command.ExecuteScalar().ToString(), out id)) { // use id } 要么 int? id = (int?)command.ExecuteScalar(); if(id.HasValue) { // use id.Value } 要么 int? id = command.ExecuteScalar() as int?; if(id.HasValue) { // use id.Value }