简体   繁体   English

从C#将十进制数存储到数据库中

[英]Store decimal number into database from c#

I am trying to store decimal numbers into a SQL Server database where column datatype is numeric(10,2) . 我试图将十进制数字存储到SQL Server数据库中,列数据类型为numeric(10,2)

The data passed from textbox to database using parameterized procedure as below 使用参数化过程将数据从文本框传递到数据库,如下所示

cmd.Parameters.Add(new SqlParameter("@PP", SqlDbType.Decimal)).Value = decimal.Parse(TB_PP.Text);

My problem it that I can enter decimal number directly into database table and when read it from database to textbox I get an decimal number 我的问题是我可以直接在数据库表中输入十进制数,当从数据库读取它到文本框时,我得到一个十进制数

But when storing it through textbox it save an integer so what should I use instead of SqlDbType.Decimal ? 但是,当通过文本框存储它时,它会保存一个整数,所以我应该使用什么代替SqlDbType.Decimal

尝试使用双精度而不是十进制:

double.Parse(TB_PP.Text);

Try defining your Parameter this way: 尝试通过以下方式定义参数:

cmd.Parameters.Add(new SqlParameter("@PP", SqlDbType.Decimal) {
    Precision = 10, 
    Scale = 2 
 });

My experience is when the SQL data type is decimal(10,2) and not numeric , but give it a try. 我的经验是,当SQL数据类型为decimal(10,2)而不是numeric ,请尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM