如何将输入文件中的行分为两部分,然后将其与c#中数据库表的两列数据进行比较?

一世

给定一个文本文件,我将如何读取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 = " ??" );
sab669

只需使用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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将 $_POST 值分为两部分并将其插入数据库

来自分类Dev

如何将Pandas数据框中的一列分为两部分并保持其余部分不变

来自分类Dev

C#如何将文本文件的行分成相等的两部分,并在两个不同的列表框中显示

来自分类Dev

根据列值中第一个出现的内容将数据框列分为两部分

来自分类Dev

将r中的数据帧分成两部分

来自分类Dev

如何基于Spark-scala中的过滤器将数据集分为两部分

来自分类Dev

如何将句子分为两部分

来自分类Dev

jekyll:如何将内容分为两部分?

来自分类Dev

如何将框架分为两部分

来自分类Dev

如何将句子分为两部分

来自分类Dev

如何将文件分成两部分然后只搜索后半部分?

来自分类Dev

按列值将数据帧分为两部分

来自分类Dev

根据列变量将数据集分为两部分,顺序很重要

来自分类Dev

按列值将数据帧分为两部分

来自分类Dev

如何根据knex.js中的第三列将一列分为两部分?

来自分类Dev

按模式将文件分为两部分

来自分类Dev

将页面分为两部分

来自分类Dev

查询以将行分为两部分

来自分类Dev

Python readlines()将行分为两部分

来自分类Dev

将熊猫列分为两部分

来自分类Dev

查询将一列分为两部分

来自分类Dev

python-将CSV列分为两部分

来自分类Dev

如何使用Insert Into将字段分为两部分?

来自分类Dev

如何将现有的diff分为两部分?

来自分类Dev

PHP:如何将数组分为两部分?

来自分类Dev

如何将字符串分为两部分

来自分类Dev

如何将环境变量语句分为两部分(VAR,VALUE)?

来自分类Dev

如何将无序列表分为两部分?

来自分类Dev

如何将列表分为两部分以填充不同的列表视图?

Related 相关文章

热门标签

归档