【问题标题】:convert string to decimal before update a table or insert data into table在更新表或将数据插入表之前将字符串转换为十进制
【发布时间】:2012-12-30 08:10:08
【问题描述】:

我想问你如何在更新表格或将数据插入表格之前,用户插入一行之后,或从dataGridView更新一行时将字符串转换为小数。

例如,我把一个十进制值2.2放到价格列,然后我把它保存到数据库,然后我刷新表,值是2,而不是@ 987654323@,或者添加2.8,那么表格显示的是3,而不是2.8。

在我的情况下,我希望价格列是十进制的,我这样做如下,但它不起作用,请帮助,谢谢。

在form1.css中:

..............
         //after a user click the save button
         private void btnSave_Click(object sender, EventArgs e) 
         { 
             Infor.UpdateDataSet((DataSet)dataGridView1.DataSource); 
          }
.........

Infor.css

  pInsert[3] = new SqlParameter("@p4", SqlDbType.Decimal, 40,
                                "price");
  pUpdate[2] = new SqlParameter("@p4", SqlDbType.Decimal, 40,
                                "price");

…………

   public static void UpdateDataSet(DataSet ds)
   {
      SqlConnection cnn = new SqlConnection(strConn);

      string sqlInsert, sqlUpdate, sqlDelete;
      sqlInsert = "insert into customers(customerid,companyname,
         contactname,price) values(@p1,@p2,@p3,@p4)";
      sqlUpdate = "update customers set companyname=@p2,
         contactname=@p3,price=@p4 where customerid=@p1";
      sqlDelete = "delete from customers where customerid=@p1";

      SqlParameter[] pInsert = new SqlParameter[4];
      SqlParameter[] pUpdate = new SqlParameter[4];
      SqlParameter[] pDelete = new SqlParameter[1];

      pInsert[0] = new SqlParameter("@p1", SqlDbType.VarChar,  5,
                                    "CustomerID");
      pInsert[1] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
                                    "CompanyName");
      pInsert[2] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
                                    "ContactName");
      pInsert[3] = new SqlParameter("@p4", SqlDbType.Decimal, 40,
                                    "price");

      pUpdate[0] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
                                    "CompanyName");
      pUpdate[1] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
                                    "ContactName");
      pUpdate[2] = new SqlParameter("@p4", SqlDbType.Decimal, 40,
                                    "price");
      pUpdate[3] = new SqlParameter("@p1", SqlDbType.VarChar,  5,
                                    "CustomerID");

      pDelete[0] = new SqlParameter("@p1", SqlDbType.VarChar,  5,
                                    "CustomerID");

      SqlCommand cmdInsert = new SqlCommand(sqlInsert,cnn);
      SqlCommand cmdUpdate = new SqlCommand(sqlUpdate,cnn);
      SqlCommand cmdDelete = new SqlCommand(sqlDelete,cnn);

      cmdInsert.Parameters.AddRange(pInsert);
      cmdUpdate.Parameters.AddRange(pUpdate);
      cmdDelete.Parameters.AddRange(pDelete);

      SqlDataAdapter da = new SqlDataAdapter();
      da.InsertCommand  = cmdInsert;
      da.UpdateCommand  = cmdUpdate;
      da.DeleteCommand  = cmdDelete;
      da.Update(ds, "customers");
      ds.AcceptChanges();
   }

【问题讨论】:

  • 不客气。另外,由于您是 StackOverflow 的新手,我想通知您,您可以通过勾选答案旁边的勾号来为好的答案投票并接受对您帮助最大的答案。在本网站上,点赞或接受的答案都算作“感谢”。

标签: c# sql winforms ado.net


【解决方案1】:

小数有两个因素,小数位和精度。

精度是数字可以包含的位数。

刻度是小数位数。

例子

  • 1111.111(精度:7,刻度3)
  • 11.11111(精度:7,刻度5)

您在创建参数时没有表示比例,只有一个精度(编辑:您实际上是在构造函数中指定大小(以字节为单位,而不是精度)。您需要显式设置 Precision 和 Scale 属性以获得您想要的结果。

例子

取自http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.scale.aspx。

SqlParameter parameter = new SqlParameter("Price", SqlDbType.Decimal);
parameter.Value = 3.1416;
parameter.Precision = 8;
parameter.Scale = 4;

来自上述来源的相关说明:

如果 Scale 属性没有明确显示,数据可能会被截断 指定且服务器上的数据不适合刻度 0( 默认)。

同样适用于表中的列定义,精度和小数位数也必须指定,当然还要匹配。 (添加了来自@OlivierJacot-Descombes 的以下评论)

【讨论】:

  • 是的,并且表格列也必须具有适当的比例。 CREATE TABLE customers (..., price DECIMAL(10, 2), ...)
  • 同意并支持@OlivierJacot-Descombes,补充答案。
  • 感谢您的帮助,我知道了。
  • @Kam2012 不用担心,很高兴为您提供帮助。如果这是正确答案或对您有帮助,请标记为答案。
  • 你好;我现在收到一些错误消息,“System.ArgumentException 未处理消息 = 参数值 '11.000' 超出范围。源 = System.Data”。 pInsert[3] = new SqlParameter("@p4", SqlDbType.Decimal, 18, "Price"); pInsert[3].Scale = 3;请告诉我为什么?
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 2017-05-16
相关资源
最近更新 更多