【问题标题】:Sending Data to SQL after x amount of ¦ lines [closed]在 x 行 ¦ 行后将数据发送到 SQL [关闭]
【发布时间】:2018-04-05 11:37:16
【问题描述】:

我是 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();

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

谢谢

【问题讨论】:

  • 你的意思是你想在数据库中保存每 7 行?
  • define “将数据发送到 sql” - 这很模糊; 7 这里的意义是什么?上下文是什么?这里的行(lines)和列(strArr)有什么区别?这一切都非常模糊。但是要回答“如果可能的话”这个问题:是的,绝对的; 如何取决于上下文
  • “一行文本中有 7 条管道”是什么意思?根据定义,一行文本不能包含多行。
  • @Binarus 我猜这里的“管道”只是意味着:|
  • @Marc Gravell 我明白了。谢谢你。他的意思是管道字符。

标签: c# sql ado.net


【解决方案1】:

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

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

如果我是正确的,那么您想像这样修改现有代码:

...
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);
}
...

【讨论】:

  • 刚刚稍微修改了一下就可以使用了,非常感谢
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2021-04-05
  • 2013-09-19
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
相关资源
最近更新 更多