在 x 行 ¦ 行后将数据发送到 SQL

Sending Data to SQL after x amount of ¦ lines

我是 C# 的新手,我想知道是否可以在一行文本中的 7 个左右管道字符 ('|') 后将数据发送到 sql,

我目前有以下

// Read each line of the file into a string array. Each element of the array is one line of the file.
string[] lines = System.IO.File.ReadAllLines(@"C:\WatchFolder\WriteLines2.txt");

// Display the file contents by using a foreach loop.

int count = 0;

char[] splitchar = { '|' };
System.Console.WriteLine("Contents of WriteLines2.txt = ");
foreach (string line in lines)
{
    string[] strArr = null;

    strArr = line.Split(splitchar);
    int iLen = strArr.Length - 1;
    for (count = 0; count <= iLen; count++)
    {
        Console.WriteLine(strArr[count]);
    }

}

// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();

上面目前显示的是一个文本文件的内容,然后在控制台每行输出所有数据。

谢谢

你的问题(即使在评论中进行了澄清)并不完全清楚,但这是我目前的猜测:

您有一个包含多行竖线 | 分隔数据的文本文件。对于每一行数据,您希望捕获前七个值,并丢弃其余值。捕获的数据应发送到数据库进行进一步处理。

如果我是对的,那么你想像这样修改你现有的代码:

...
string[] strArr;
string[] newArray = new string[7];  

foreach (string line in lines)
{
    strArr = line.Split(splitchar);
    Array.Copy(strArr, newArray, 7);

    // Call DB function, passing newArray
    saveToDB(newArray);
}
...