CSV到DataTable列被限制为唯一异常

时间:2017-07-03 07:14:10

标签: c# csv datatable

我使用以下代码将CSV文件转换为DataTable:

ERROR TypeError: Cannot read property 'myViewProperty' of undefined
    at Object.eval [as updateDirectives] (MyViewComponent.html:23)
    at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:12806)
    at checkAndUpdateView (core.es5.js:12144)
    at callViewAction (core.es5.js:12507)
    at execComponentViewsAction (core.es5.js:12439)
    at checkAndUpdateView (core.es5.js:12150)
    at callViewAction (core.es5.js:12507)
    at execEmbeddedViewsAction (core.es5.js:12465)
    at checkAndUpdateView (core.es5.js:12145)
    at callViewAction (core.es5.js:12507)

我在运行时遇到以下异常:

专栏' UniqueId'被限制为独特的。价值' 1'已经存在。

为什么我会得到这个例外?

注意:这是数据存储在CSV文件中的方式:

    public OverrideTableDataSet ConvertCSVtoDataTable(out OverrideTableDataSet dt, string csvFile)
    {
        Int32 num;
        dt = new OverrideTableDataSet();
        using (StreamReader sr = new StreamReader(csvFile))
        {
            string[] headers = sr.ReadLine().Split(',');
            foreach (string header in headers)
            {
                if(!dt.OverrideDataTable.Columns.Contains(header))
                {
                    dt.OverrideDataTable.Columns.Add(header);
                }
            }

            while(!sr.EndOfStream)
            {
                string[] rows = sr.ReadLine().Split(',');
                DataRow dr = dt.OverrideDataTable.NewRow();
                for (int i = 0; i < headers.Length; i++)
                {
                    rows[i] = rows[i].Replace("\\", "").Replace("\"", "");
                    dr[i] = Int32.TryParse(rows[i], out num);
                }
                dt.OverrideDataTable.Rows.Add(dr);
            }
        }

        return dt;
    }

0 个答案:

没有答案