C# 中 foreach 循环迭代中的列不属于数据表。
我正在将行记录插入到数据表中,但是面对的列不属于数据表。
//Prepare Datatable and Add All Columns Here
DataTable dt = new DataTable();
DataRow row;
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "title";
dc.ReadOnly = false;
dc.Unique = true;
dc.AutoIncrement = false;
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "link";
dc.ReadOnly = false;
dc.Unique = true;
dc.AutoIncrement = false;
foreach (XmlNode rssNode in rssNodes)
{
XmlNode rssSubNode = rssNode.SelectSingleNode("title");
string title = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("link");
string link = rssSubNode != null ? rssSubNode.InnerText : "";
//Add new row and assign values to columns, no need to add columns again and again in loop which will throw exception
row = dt.NewRow();
//Map all the values in the columns
row["title"] = title;
row["link"] = link;
//At the end just add that row in datatable
dt.Rows.Add(row);
}
富国沪深
相关分类