【问题标题】:How to return only the integer part of a decimal value from a Sql Server database?如何从 Sql Server 数据库中只返回十进制值的整数部分?
【发布时间】:2022-06-18 00:40:35
【问题描述】:

我只需要返回通过来自 SQL Server 视图的 http 请求收到的数字的整数部分,例如:

如果数据库中的数字是 1234,我需要接收值 1234,但 asp net 框架返回 1234.0。
小数点后的零是我的问题。

尽管我的列是十进制格式,但所有保存的值都是整数。我无法更改此视图的列数据类型。

我的视图仅由两列组成:
PERSON_CODE(十进制(6,0),空)
PERSON_NAME (varchar(115), null)

我已经尝试使用 [DisplayFormat(...,但没有区别,我的返回总是小数点后的 0

    public class VW_PERSONS
{

    [Key]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0}")]
    public decimal PERSON_CODE { get; set; }


    public string PERSON_NAME { get; set; }

}

我的控制器:

        [HttpGet]
    public object GetPersonByCode(decimal PERSON_CODE)
    {
        try
        {
            return new Business.Person().GetPersonByCode(PERSON_CODE);
        }
        catch (Exception ex)
        {
            throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Util.GetExceptionMessages(ex)));
        }
    }

在我的企业内部:

        internal PersonModel GetPersonByCode(string PERSON_CODE)
    {
        using (ExtranetEntities extranet = new ExtranetEntities())
        {
            int CODE_PERSON= int.Parse(PERSON_CODE);
            return new ContatoModel(extranet.VW_PERSONS.FirstOrDefault(r => r.PERSON_CODE == CODE_PERSON));
        }
    }

谁能帮我解决这个问题?

【问题讨论】:

标签: c# .net asp.net-mvc .net-4.8


猜你喜欢
  • 1970-01-01
  • 2011-10-14
  • 2011-12-30
  • 1970-01-01
  • 2013-04-18
  • 1970-01-01
  • 2011-07-12
  • 2011-06-25
  • 2016-08-16
相关资源
最近更新 更多