C#DataTable列已存在问题

C#DataTable列已存在问题,c#,csv,datatable,C#,Csv,Datatable,我试图将CSV文件导入数据表,但是CSV包含相同的标题。(例如,不同表单部分有多个“日期”标题)。为了解决这个问题,我决定创建一个循环,该循环将遍历标题并根据它们的位置替换重复项或不需要的条目。我已经用伪条目替换了replaceWith数组,但是我的实际代码确实具有与replace数组关联的正确大小 string[] columnNames = null; string[] oStreamDataValues = null; int[] error = {0,1,

我试图将CSV文件导入数据表,但是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);
                    }
                }
            }
string[]columnNames=null;
字符串[]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,9910310711011111712112412412712813013218221022114216222239};
int[]替换={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};
字符串[]replaceWith={“替换1”、“替换2”、“替换3”};
字符串fix=“忽略”;
int inc=00;
字符串条目=”;
而(!oStreamReader.EndOfStream)
{
字符串oStreamRowData=oStreamReader.ReadLine().Trim();
如果(oStreamRowData.Length>0)
{
//oStreamDataValues=Regex.Split(oStreamRowData,,(?=(?:[^']*'[^']*')*[^']*')*[^']*$);
oStreamDataValues=oStreamRowData.Split(',');
如果(行计数==0)
{
行数=1;
columnNames=oStreamDataValues;
for(int i=0;i
我不是编码专家,所以我的语法/决策并不完美

但是,我得到的错误是,名为“ignore_4”的列已经属于此数据表


我假设我的循环逻辑中有错误。

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

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);
}
string rep=“replace”;//替换字段的基本字符串
string fix=“ignore”;//ignore字段的基本字符串
//为了演示,我已经注释掉了这个数组
//希望每个“替换”列都有其特定的名称,然后准备此
//数组中的名称数与
//替换数组中的元素
//
//字符串[]replaceWith={“替换1”、“替换2”、“替换3”};
int idxErrors=0;//错误数组中的当前位置
int idxReplace=0;//替换数组中的当前位置
int fixCounter=1;
int repCounter=1;
字符串条目=”;
for(int i=0;i

在本例中,我对忽略列使用了相同的模式,也对需要更改名称的列使用了相同的模式。如果要为每个重命名列指定一个正确的名称,则需要使用这些正确的名称准备一个数组,并且该数组的长度应与替换数组的长度相同。然后使用
idxReplace
从可能的专有名称数组中获取正确的名称。

您能解释一下数组错误和数组替换中的数字是什么意思吗?数组错误表示CSV文件(在本例中为columnNames)中我要替换为“忽略”的位置号。replace数组表示我希望用replaceWith数组值替换的columnNames中的位置号。因此,当出现错误[1],其值为1时,代码将用ignore_01替换columnNames[1]