如何跳过文本文件中的特定行到DataTable [英] How to skip specific line in text file to DataTable
本文介绍了如何跳过文本文件中的特定行到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在将一个csv文件读入一个sql表。
我需要跳过第一个始终以字符串STR开头的行,例如STR122,以及始终为END的最后一行。
我能忽略子串STR,但是子字符串END有问题。
我不希望将END写入DataTable
我的代码如下
Hi
I am reading a csv file into a sql table.
I need to skip the the first line which always starts with the string STR, eg "STR122", and the last line which is always END.
I was able ignore the substring "STR", but having problems with the substring "END".
I don't want "END" to be written into the DataTable
My code is as follows
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
bool endOfTransaction = false;
string csvData = File.ReadAllText(filePath);
dc.ColumnName = "Number";
dt.Columns.Add(dc);
foreach (string row in csvData.Split('\r'))
{
if (!string.IsNullOrEmpty(row) && row.Substring(0,3) != "STR")
{
if ((row.Substring(0, 3) == "END"))
{
Console.WriteLine("End of file");
endOfTransaction = true;
}
else
{
dt.Rows.Add(row);
}
}
}
SqlBulkCopy bc = new SqlBulkCopy(con,SqlBulkCopyOptions.TableLock, null);
bc.DestinationTableName = "bulkAff_EmpNumbers";
bc.WriteToServer(dt);
bc.Close();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "[dbo].[chuck_doQAFF_C#]";
cmd.Parameters.Add(new SqlParameter("@DebugMode", 1));
cmd.CommandTimeout = 50000;
SqlDataReader extractReader = cmd.ExecuteReader();
WriteToFile file = new WriteToFile();
file.parseResultSetToFile(extractReader);
推荐答案
Quote:
if(row.row.Substring(0,3)!=END)
if (row.row.Substring(0,3) != "END")
不应该只是简单地
Shouldn't that be simply
if (row.Substring(0,3) != "END")
?
这篇关于如何跳过文本文件中的特定行到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文