我有一个.txt文件。包含INSERT脚本。
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3) INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
and so on...
我想读取此文件并分别输出每个INSERT语句。 INSERT脚本可能不会被换行分隔。它可以在同一行中继续。可以看到最后一个INSERT脚本。
因此,我想知道是否可以阅读此文本文件,并在每当找到新的“插入”字词时将每一行分开?
答案 0 :(得分:0)
@Sayse提供了一些答案提示。您可以在这里执行以下操作:
public static void Main()
{
// search how to read content from a file in C# in case of doubt
var content = @"INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3) INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)";
// choose your delimiter
const string delimiter = "INSERT INTO TABLE ";
var inserts = content
.Split(new []{delimiter}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => delimiter + x.TrimEnd());
foreach (var insert in inserts)
Console.WriteLine(insert);
}
输出
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)
INSERT INTO TABLE (COL1, COL2, COL3) VALUES (1, 2, 3)