C # What is the parameter sqldbtype. Decimal? Specify the precision and decimal places

Source: Internet
Author: User

In. when operating databases in. net, we recommend that you use parameterization to pass values. However, this may cause some doubts, for example, how can we maintain the same precision and decimal places as in the database when using decimal?

You can use the precision and scale attributes of the parameter, for example:

/// <Summary>
/// Update a piece of data
/// </Summary>
Public dictionaryentry update_dictionaryentry (sderp. model. storehouse. storage_goodsunitexchange Model)
{
......
Sqlparameter [] parameters = {
New sqlparameter ("@ ID", sqldbtype. Int, 4 ),
New sqlparameter ("@ gid", sqldbtype. varchar, 50 ),
New sqlparameter ("@ changerate", sqldbtype. decimal, 13)
};
Parameters [0]. value = model. ID;
Parameters [1]. value = model. GID;
Parameters [2]. value = model. changerate;
Parameters [2]. Precision = 28;
Parameters [2]. Scale = 10;

Return new dictionaryentry (strsql, parameters );
}

In this way, the precision of the "@ changerate" parameter is set to 28, and the maximum number of decimal places is 10.

note: in fact. in net, if there are no special circumstances, we do not need to set the precision and decimal places for the parameter. By default, the parameter will truthfully pass the user's value, if the number of decimal places is not set as in the database, the number of decimal places is 0. and if the precision and decimal places are set in the Program , later program modifications will also bring about a lot of Work . Just think about it. If we do not set it here, we will change the decimal scale in the future, you just need to get rid of the corresponding field length in the database, and the program does not need to make any changes. If we also set the number of decimal places in the program, we have to modify the program while modifying the database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.