简体   繁体   English

将固定宽度的文本文件逐行读取到数据表中-C#

[英]Read fixed width text file into datatable row by row - C#

I have a large fixed width text file, I want to read it row by row and insert into a datatable, or just read it into a datatable. 我有一个大的固定宽度文本文件,我想逐行读取并将其插入到数据表中,或者只是将其读取到数据表中。 how can I assign that from which to which position one column will be? 如何将一列从哪个位置分配到哪个位置?

I also have to perform some action like trim the extra space of columns for every record. 我还必须执行一些操作,例如为每条记录修剪额外的列空间。

I tried this code but it gives me datatable with just 3 columns instead of 15 我尝试了这段代码,但是它给我的数据表只有3列而不是15列

string sourcePath = @"c:\\";
string filename = "file.txt";

DataTable dt;

//Create OleDb connection object
using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;" +
            "Data Source=" + sourcePath + ";Extended Properties=\"Text;\""))
{
    // Open connection 
    cn.Open();

    // Create OleDb Adapter object 
    using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + filename, cn))
    {
        dt = new DataTable("Records");
        adapter.Fill(dt);

        // Display results
        dataGridView1.DataSource = dt;
    }
}

Sample Data 样本数据

Sample Name.......~Address~City~State~12345 Contact Name 123-456-7890 Type Payment Sample Name.......~Address~City~State~12345 Contact Name 123-456-7890 Type Payment Sample Name.......~Address City~State~12345 Contact Name 123-456-7890 Type Payment Sample Name.......~Address City~State~12345 Contact Name 123-456-7890 Type Payment 样本名称.......〜地址〜城市〜州〜12345联系人姓名123-456-7890类型付款样本名称.......〜地址〜城市〜州〜12345联系人姓名123-456-7890类型付款样品名称.......〜地址城市〜州〜12345联系人姓名123-456-7890类型付款样品名称.......〜地址城市〜州〜12345联系人姓名123-456-7890类型付款

There is a lot spaces in between.. 之间有很多空间。

If you want to read your text file using OleDb Text Driver you need to provide a Schema.ini file that describe the structure of one of the "records" (a line) of your text file 如果要使用OleDb文本驱动程序读取文本文件,则需要提供一个Schema.ini文件,该文件描述文本文件的“记录”(一行)之一的结构。

So supposing that your text file contains records in this format: 因此,假设您的文本文件包含以下格式的记录:

XXXXXYYYYYKKKKKZZZZ

(Four fields, each of 5 char) you need to add in the same folder where is located your text file a Schema.ini file with this structure (四个字段,每个为5个字符),您需要在文本文件所在的同一文件夹中添加具有此结构的Schema.ini文件

[file.txt]
Format=FixedLength
Col1=CustomerNumber Text Width 5
Col2=CustomerName Text Width 5
Col3=CustomerCity Text Width 5
Col4=CustomerZIP Text Width 5

You could find more detail at the MSDN page linked above 您可以在上面链接的MSDN页面上找到更多详细信息

Without using OleDb you need to use something already available in the NET Framework, or write your own splitting code, or use a thirdy party library 在不使用OleDb的情况下,您需要使用NET Framework中已经可用的东西,或者编写自己的拆分代码,或者使用第三方库。

For your own code approach I would start defining a class for your record. 对于您自己的代码方法,我将开始为您的记录定义一个类。
For example an Customer class could be 例如,客户类可以是

public class Customer
{
    public string Name {get;set;}
    public string Address {get;set;}
    ... other fields omitted ....
}

then you define a List<Customer> and start reading your file with 然后定义一个List<Customer>并开始读取文件

List<Customer> customers = new List<Customer>();
foreach(string line in File.ReadLines(filename)
{
    Customer c = new Customer();
    c.Name = line.Substring(0, 10).Trim();
    c.Address = line.Substring(10, 15).Trim();
    ... and so on for all the other fields....

    customers.Add(c);        
}

At the end of the loop you have your list filled with your data. 在循环的最后,您的列表中充满了数据。 Of course if you just need to write everything in another table on a database server you could write the code that inserts/updates the database table directly inside the loop and avoid the loading of the list. 当然,如果您只需要在数据库服务器上的另一个表中编写所有内容,则可以编写直接在循环内部插入/更新数据库表的代码,并避免加载列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM