IM通过数据集试图环和地方曾经有一个0我想改变这种状况为空值或空白到目前为止,我有这个如何通过数据集和更改数据的C#循环
foreach (DataRow drRow in ds.Tables[0].Rows)
{
//loop through cells in that row
{
//if the value is 0 change to null or blank
}
}
能任何人都可以帮我解决这个问题吗?
IM通过数据集试图环和地方曾经有一个0我想改变这种状况为空值或空白到目前为止,我有这个如何通过数据集和更改数据的C#循环
foreach (DataRow drRow in ds.Tables[0].Rows)
{
//loop through cells in that row
{
//if the value is 0 change to null or blank
}
}
能任何人都可以帮我解决这个问题吗?
当然
foreach (DataRow drRow in ds.Tables[0].Rows)
{
for(int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
int rowValue;
if (int.TryParse(drRow[i].ToString(), out rowValue))
{
if (rowValue == 0)
{
drRow[i] = null;
}
}
}
}
我真的相信,没有人仍与数据集:(
像这样的东西可能有帮助的工作原理:
foreach (DataRow rows in table.Rows)
{
object value = null;
var cells = rows.ItemArray;
for (int i = 0; i < cells.Length; i++)
{
value = cells[i];
if (value != null && value.GetType() == typeof(int))
{
if ((int)value == 0)
{
cells[i] = null;
}
}
}
rows.ItemArray = cells;
}
你可以做
DataTable dt = new DataTable();
.....
.....
DataRowCollection drColl = dt.Rows;
for(int j=0; j<drColl.Count;j++)
{
DataRow drRow = drColl[j];
object[] itemArr = null;
itemArr = drRow.ItemArray;
//loop through cells in that row
for(int i=0; i<itemArr.Length; i++)
{
//if the value is 0 change to null or blank
if (itemArr[i].ToString() == "0")
drRow[i] = "";
}
}
如果任务是取代所有的zeroe用'NULL'写出SQL UPDATE语句并执行脚本。将数据加载到'dataset'中的目的是什么?为什么你使用C#。 IMO SQL脚本是更好的解决方案 – 2011-06-16 12:28:26