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.