繁体   English   中英

C#DataTable列已存在问题

[英]C# DataTable column already exists issue

我正在尝试将CS​​V文件导入到DataTable中,但是CSV包含相同的标头。 (例如,对于不同的表单节有多个“日期”标头)。 为了解决这个问题,我决定创建一个循环,循环通过标题,并根据重复位置或不需要的条目替换它们的位置。 我已经用虚拟条目替换了replaceWith数组,但是我的实际代码确实具有与replace数组相关的正确大小。

string[] columnNames = null;
        string[] oStreamDataValues = null;
        int[] error = {0,1,2,3,4,7,8,9,10,11,15,21,34,37,57,61,65,68,69,71,75,79,82,83,85,89,93,96,97,99,103,107,110,111,113,117,121,124,125,127,128,129,130,132,182,210,212,213,214,215,216,222,226,239};
        int[] replace = {14,16,17,17,20,23,24,27,28,29,31,32,44,58,59,60,62,63,64,66,67,70,72,73,74,76,77,78,80,81,84,86,87,88,90,91,92,94,95,98,100,101,102,104,105,106,108,109,112,114,115,116,118,119,120,122,123,126,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,184,186,187,188,190,191,192,194,195,196,198,199,200,202,203,204,206,207,208,209,236,242,243,244};
        string[] replaceWith = {"Replace 1", "Replace 2", "Replace 3"};
        string fix = "ignore_";
        int inc = 00;
        string entry = "";
        while (!oStreamReader.EndOfStream)
        {
            string oStreamRowData = oStreamReader.ReadLine().Trim();
            if (oStreamRowData.Length > 0)
            {
                //oStreamDataValues = Regex.Split(oStreamRowData, ",(?=(?:[^']*'[^']*')*[^']*$)");
                oStreamDataValues = oStreamRowData.Split(',');
                if (rowCount == 0)
                {
                    rowCount = 1;
                    columnNames = oStreamDataValues;

                        for (int i = 0; i < columnNames.Length; i++)
                        {
                            for (int j = 0; j < error.Length; j++)
                            {
                                if (error[j] == i)
                                {
                                    entry = fix + inc++;


                                }
                            }
                            for (int k = 0; k < replace.Length; k++)
                            {

                                if (replace[i] == i)
                                {
                                    int add = 0;
                                    entry = replaceWith[add++];
                                }
                            }


                        DataColumn oDataColumn = new DataColumn(entry, typeof(string));
                        oDataColumn.DefaultValue = string.Empty;
                        oDataTable.Columns.Add(oDataColumn);
                    }
                }
            }

我不是编码专家,因此我的语法/决策制定并不完美。

但是,我得到的错误是名为“ ignore_4”的列已属于此DataTable。

我认为循环逻辑中有错误。

我认为您使循环过于复杂了。 您只需要在错误数组和替换数组中保留当前位置的索引。

string rep = "replace_";  // base string for replace fields
string fix = "ignore_";   // base string for ignore fields

// For demonstation purpose I have commented out this array. If you 
// want every 'replace' column have its specific name then prepare this
// array with exactly the number of names required by the number of 
// elements in the replace array
//
// string[] replaceWith = {"Replace 1", "Replace 2", "Replace 3"};

int idxErrors = 0;        // Current position in the error array
int idxReplace = 0;       // Current position in the replace array
int fixCounter = 1;
int repCounter = 1;
string entry = "";
for (int i = 0; i < columnNames.Length; i++)
{
    // Is this the index of a column that should be ignored?
    if (idxErrors < error.Length && i == error[idxErrors])
    {
        entry = fix + fixCounter.ToString("D2");
        idxErrors++;
        fixCounter++;
    }
    // Is this the index of a column that should have a different name??
    else if (idxReplace < replace.Length && i == replace[idxReplace])
    {
        entry = rep + repCounter.ToString("D2");
        // entry = replaceWith[repCounter];
        idxReplace++;
        repCounter++;
    }
    else
        entry = columnNames[i];

    // Now create the column
    DataColumn oDataColumn = new DataColumn(entry, typeof(string));
    oDataColumn.DefaultValue = string.Empty;
    oDataTable.Columns.Add(oDataColumn);
}

在此示例中,我对被忽略的列使用了相同的模式,对于需要更改名称的列也使用了相同的模式。 如果要给每个重命名的列取一个适当的名称,则需要准备一个具有这些适当名称的数组,并且此数组的长度应与replace数组的长度相同。 然后,使用idxReplace从可能的专有名称数组中获取正确的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM