给定一个文本文件,我将如何读取line中的特定数字。
说,我有一个文件123.txt。我将如何读取行号并将前5位数字存储在其他变量中,然后将6位数字存储到另一个变量中。
我所看到的只是涉及将整个文本文件存储为String数组的东西。但是有一些复杂性:文本文件非常大,并且我正在编写应用程序的机器也不是一流的系统。速度不是重中之重,但这绝对是一个主要问题。
//请在这里帮助
// Want to compare data of input file with database table columns.
// How to split data in to parts
// Access that split data later for comparison.
// Data in input file is like,
//
// 016584824684000000000000000+
// 045787544574000000000000000+
// 014578645447000000000000000+
// 047878741489000000000000000+ and so on..
string[] lines = System.IO.File.ReadAllLines("F:\\123.txt"); // Input file
// How can I divide lines from input file in 2 parts (For ex. 01658 and 4824684) and save it in variable so that I can use it for comparing later.
string conStr = ConfigurationManager.ConnectionStrings["BVI"].ConnectionString;
cnn = new SqlConnection(conStr);
cnn.Open();
// So I want to compare first 5 digits of all lines of input file (ex. 01658)with Transit_ID and next 6 digits with Client_Account and then export matching rows in excel file.
sql = "SELECT Transit_ID AS TransitID, Client_Account AS AccountNo FROM TCA_CLIENT_ACCOUNT WHERE Transit_ID = " //(What should I put here to comapare with first 5 digits of all lines of input file)" AND Client_Account = " ??" );
只需使用Substring(int32, int32)
即可获取如下所示的适当值:
string[] lines = System.IO.File.ReadAllLines("F:\\123.txt");
List<string> first = new List<string>();
List<string> second = new List<string>();
foreach (string line in lines)
{
first.Add(line.Substring(0, 5));
second.Add(line.Substring(6, 6));
}
尽管埃里克的答案要干净得多。这只是使用样本数据的快速而肮脏的概念证明。您一定要使用该using
声明,并StreamReader
按照他的建议。
first
将包含来自中每个元素的前5位lines
,并second
包含后6位。
然后,要构建您的SQL,您需要执行以下操作:
sql = "SELECT Transit_ID AS TransitID, Client_Account AS AccountNo FROM TCA_CLIENT_ACCOUNT WHERE Transit_ID = @TransitId AND Client_Account = @ClientAcct");
SqlCommand cmd = new SqlCommand(sql);
for (int i = 0; i < lines.Count; i++)
{
cmd.Parameters.AddWithValue("@TransitId", first[i]);
cmd.Parameters.AddWithValue("@ClientAcct", second[i]);
//execute your command and validate results
}
这将循环N次,并对中的每个值运行一个命令lines
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句