简体   繁体   English

读取文本文件,然后将行分为几列

[英]Reading Text Files and then breaking down lines into columns

I have an assignment where I need to read a text file and then breakdown each line into columns then I need to insert that into database. 我有一个作业,我需要阅读一个文本文件,然后将每一行细分为各列,然后将其插入数据库。

What's the best approach for this? 最好的方法是什么? Any help will be appreciate it and if you could provide code will be even better. 任何帮助将不胜感激,如果您能提供代码,那就更好了。

This is what I have so far 这就是我到目前为止

    string filename = Server.MapPath("~/Text_File_4.txt");

    StreamReader sr = new StreamReader(filename);

    string styl;
    string colr;
    string sdim;
    string size;
    string qty;
    string line;

    string sprice;
    string sretail;

    while ((line = sr.ReadLine()) != null)

    {
        styl = line.Substring(0, 6);
        colr = line.Substring(6, 2);
        sdim = line.Substring(8, 1);
        size = line.Substring(14, 3);
        qty = line.Substring(19, 5);


        sprice = line.Substring(27, 6);
        sretail = line.Substring(38, 4);

        con.Open();
        cmd = new SqlCommand("insert into ststyl00(ststyl, stcolr, stsdim, stszcd, stprq, strprq) values(@ststyl, @stcolr, @stsdim, @stszcd, @stprq, @strprq)", con);

        cmd.Parameters.Add("@ststyl", SqlDbType.VarChar, 15).Value = styl;
        cmd.Parameters.Add("@stcolr", SqlDbType.VarChar, 3).Value = colr;
        cmd.Parameters.Add("@stsdim", SqlDbType.VarChar, 8).Value = sdim;
        cmd.Parameters.Add("@stszcd", SqlDbType.VarChar, 3).Value = size;

        cmd.Parameters.Add("@stprq", SqlDbType.VarChar, 8).Value = sprice;
        cmd.Parameters.Add("@strprq", SqlDbType.VarChar, 8).Value = sretail;
        cmd.ExecuteNonQuery();
        con.Close();



    }

Input is a CSV 输入的是CSV

If your input files are CSV files, I strongly recommend using the CSV Reader class available at 如果您的输入文件是CSV文件,我强烈建议您使用CSV阅读器类,网址为

http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

Input is Fixed-Width 输入为固定宽度

If your input is fixed-width, just read all of the lines in and parse each individual line into an appropriate structure to store in the database (more on that in a moment). 如果您输入的内容是固定宽度的,则只需阅读其中的所有行,并将每行都解析为适当的结构以存储在数据库中(稍后会介绍更多内容)。

If you have just a little text to read (perhaps a few megabytes or less), just use 如果您只有一点文字要阅读(也许只有几兆字节或更小),请使用

File.ReadAllLines

http://msdn.microsoft.com/en-us/library/s2tte0y1 http://msdn.microsoft.com/en-us/library/s2tte0y1

to read in all of the lines of the file into a string[] . 将文件的所有行读入string []

Writing to the DB 写入数据库

You now have a capability to read in the file. 您现在可以读取文件。 Now, you need to write it out to the database. 现在,您需要将其写出到数据库中。 Presumably there is a DB table with a given schema that matches the data in the file. 大概有一个具有给定模式的数据库表,该表与文件中的数据匹配。 Have a look at ADO.Net to understand how to write to a database and ask specific questions as needed. 看一下ADO.Net,了解如何写入数据库并根据需要提出特定问题。

http://msdn.microsoft.com/en-us/library/h43ks021(v=vs.100).aspx http://msdn.microsoft.com/zh-CN/library/h43ks021(v=vs.100).aspx

This sounds like you have to have the text file with delimiter. 听起来您必须使用带定界符的文本文件。 The delimiter which separates the data into columns, eg 将数据分为几列的定界符,例如

data1, data2, data3, data4 数据1,数据2,数据3,数据4

The delimiter could be comma or any other character which is not appearing into regular data. 分隔符可以是逗号或不出现在常规数据中的任何其他字符。 If you have the text file in this format, it would be easy to parse it and push it to database. 如果您使用这种格式的文本文件,将很容易解析它并将其推送到数据库。

The approach could be - You open the file using StreamReader. 方法可能是-您使用StreamReader打开文件。 Read the file line by line ie read a line at a time. 逐行读取文件,即一次读取一行。 Split the line into columns through specifying delimiter. 通过指定定界符将行分成几列。

string[] lineData = sr.ReadLine().split('delimiter');
foreach(string colData in lineData)
{
     //store data into appropriate collections and push it to database
}

除了已经建议的其他解析技术之外,您还可以将TextFieldParser类(在Microsoft.VisualBasic.FileIO名称空间中)与已编写的ADO.Net代码一起使用。

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

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