我有一个 Sql Utility 类,其中包含很多关于 Sql 查询的方便方法。该类包含以下方法:
public static T ExecuteScalar<T>(
string query,
SqlConnection connection,
params SqlParameter[] parameters)
where T : class, new()
{
SqlCommand command =
CreateCommand(query, connection, parameters);
return command.ExecuteScalar() as T;
}
是否可以返回例如Guid
对象或其他不可为空的类。
像这样的东西:
Guid result =
SqlUtils.ExecuteScalar<Guid>(
@"SELECT [Id] FROM [dbo].[MyTable]
WHERE [Column1] = @Param1",
connection,
new SqlParameter("@Param1", "someValue"));