" String.Format(" {0:n}",)"尝试更新数据表时,函数不起作用。 [英] "String.Format("{0:n}", )" function is not working while trying to update data table.

查看:74
本文介绍了" String.Format(" {0:n}",)"尝试更新数据表时,函数不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改数据表中值的数字格式。我使用String.Format({0:n},数字)方法更改数字格式但它不起作用。

下面是我的代码,

  foreach (DataRow dr  in  datasetx.Tables [ 0 ]。行)
{

dr [ total] = 字符串 .Format( {0:n},Convert.ToDecimal(dr [ total< /跨度>]));

}
datasetx.Tables [ 0 ]。AcceptChanges();





但它不起作用。

dr [total] = 123456789.1245;

String.Format({0:n},Convert.ToDecimal(dr [total])); = 1,234,567,89.1245;正如预期的那样,数据表中的值仍然是旧的。



之前:dr [total] = 123456789.1245;

格式化后使用String.Format的字符串:dr [total] = 123456789.1245;





为什么值不反映在数据表中?

实际上String.Format({0:n},)函数按预期工作,但该值根本没有反映在数据表中。

解决方案

这是一个常见的错误:你只是用它的字符串表示来判断十进制值(存储在数据库中)。



在任何时候都不应该存储格式化数据库中的值。数据库必须保存数字;只有当你向用户显示这个号码时才会对它应用格式。


你不需要调用datasetx.Tables [0] .AcceptChanges()

喜欢这种方法会写出格式化的字符串。

  protected   void  Page_Load( object  sender,EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn( total );
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr [ total] = 123456789.1245;
dt.Rows.Add(dr);

foreach (DataRow dr1 in dt.Rows)
{

dr1 [ total] = 字符串 .Format( {0:n},Convert.ToDecimal (dr1 [ total]));
}
Response.Write(dt.Rows [ 0 ] [ < span class =code-string> total
]);
}


好吧,看看你的代码:

根据MSDN [ ^ ]:

AcceptChanges()提交自上次调用AcceptChanges以来对此表所做的所有更改。

调用AcceptChanges时,任何DataRow对象仍处于编辑状态模式成功结束其编辑。 DataRowState也会更改:所有已添加和已修改的行都将变为未更改,并删除已删除的行。

在尝试使用DbDataAdapter.Update方法更新DataSet后,通常会在DataTable上调用AcceptChanges方法。



含义,您的实际数据不受影响在数据库中。您只是更新数据表中的数据。如果您想在数据库中进行此更改,则需要为该特定记录编写更新语句



-KR

Am trying to change the number format of a value in a data table. Am using String.Format("{0:n}", number) method to change the number format but it doesn't work.
Below is my code,

foreach (DataRow dr in datasetx.Tables[0].Rows)
                  {

         dr["total"] = String.Format("{0:n}", Convert.ToDecimal(dr["total"]));

                  }
                  datasetx.Tables[0].AcceptChanges();



but it is not working.
dr["total"] = 123456789.1245;
String.Format("{0:n}", Convert.ToDecimal(dr["total"])); = 1,234,567,89.1245; as expected but value in data table is still the old one.

before : dr["total"] = 123456789.1245;
after formatting string using String.Format : dr["total"] = 123456789.1245;


why value is not reflecting in datatable ?
actually String.Format("{0:n}", ) function is working as expected but that value is not at all reflecting in data table.

解决方案

This is a common mistake: you just counfound the decimal value (stored in the database) with its string representation.

At no point you should store a formatted value in the database. The database has to hold the number; only when you display this number to the user you apply a format to it.


You need not to Call datasetx.Tables[0].AcceptChanges()
like this method will write formatted string.

protected void Page_Load(object sender, EventArgs e)
   {
       DataTable dt = new DataTable();
       DataColumn dc = new DataColumn("total");
       dt.Columns.Add(dc);
       DataRow dr = dt.NewRow();
       dr["total"] = "123456789.1245";
       dt.Rows.Add(dr);

       foreach (DataRow dr1 in dt.Rows)
       {

           dr1["total"] = String.Format("{0:n}", Convert.ToDecimal(dr1["total"]));
       }
       Response.Write(dt.Rows[0]["total"]);
   }


Well, looking at your code:
According to the MSDN[^]:
AcceptChanges() Commits all the changes made to this table since the last time AcceptChanges was called.
When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.
The AcceptChanges method is generally called on a DataTable after you attempt to update the DataSet using the DbDataAdapter.Update method.

Meaning, your actual data is unaffected in the database. You're just updating the data in the datatable. If you want this change in the database, you need to write the update statement for that particular record.

-KR


这篇关于&quot; String.Format(&quot; {0:n}&quot;,)&quot;尝试更新数据表时,函数不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆