我正在尝试创建一个数据表,然后向其中添加几行。这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace thisNamespace
{
class Program
{
static void Main(string[] args)
{
DataTable dt=new DataTable();
dt.Columns.Add("XYZID");
DataRow dr=dt.NewRow();
dr["XYZID"]=123;
dt.ImportRow(dr);
dr["XYZID"] = 604303;
dt.ImportRow(dr);
}
}
}
当我单步执行程序时,dr
成功初始化并填充了值,但是在 ImportRow(dr)
之后,dt 中的行数
仍然是 0。我觉得我一定遗漏了一些明显的东西。这里出了什么问题?
最佳答案
如果该行是分离的(就像它首次创建时一样),则 ImportRows 会无提示地失败(无一异常(exception))- 要能够导入行,您必须将其添加到表中。之后您可以将其导入到其他表中。
关于c# - DataTable.ImportRow 不添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15824967/