请更正此替换代码 [英] please Correct this replace code

查看:92
本文介绍了请更正此替换代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发者
我正在为数据集编写替换方法,以替换数据集中加载的csv文件中的通配符或正则表达式.

Hi developers
I am writing a replace method for a dataset to replace wildcards or regular expression from a csv file which is loaded in dataset .

foreach (DataRow datarow in dataset.Tables[0].Rows)
{
  foreach (DataColumn datacol in dataset.Tables[0].Columns)
  {
    datarow[datacol] = Regex.Replace(datarow[datacol].ToString(), comma.ToString(), slash.ToString());
    datarow[datacol] = Regex.Replace(datarow[datacol].ToString(), colon.ToString(), Blank.ToString());
  }
}


此方法很好用,但替换通配符或正则表达式后,将引发错误.
输入字符串的格式不正确,无法在PRIORITY列中存储???.预期类型为Int32"
请解决代码中出现的问题.


This method is working good but after replacing wildcards or a regular Expression an error is thrown.
"Input string was not in a Correct format ,Could not Store ??? in PRIORITY Column. Expected type Int32"
Please solve the problem that is raised in the code.

推荐答案

Vishu!

您的代码会将所有内容转换为字符串.当您写回数据行
当列包含int或float时,您会遇到问题.当错误消息告诉您时,它应该输入Int32.

在您的情况下,我要做的是检查列类型,并且仅在基于字符的列类型的情况下才转换为字符串和替换部分.

干杯

曼弗雷德(Manfred)
Hi Vishu!

Your code converts everything to a string. When you write back to the datarow
you''ll run into issues when a column contains an int or a float. As the error message is telling you, it expected type Int32.

What I''d do in your case is to check the column type and only do the conversion to string and the replacing part in case of a character based column type.

Cheers

Manfred


这篇关于请更正此替换代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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