将数据从文本文件导出到数据库. [英] Export data from a text file to database.

查看:106
本文介绍了将数据从文本文件导出到数据库.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中的内容是-

100000 Ren/100000凭证条目添加600006 2012/4/9 PARVD/12-13/0005
100001 Ren/100001凭证输入Add 600006 2012/4/9 PARVD/12-13/0006
100002 Ren/100002凭证条目添加600006 2012/4/9 PARVD/12-13/0007

大约78887行,我想通过c#

I have a text file in which the matters are--

100000 Ren/100000 Voucher Entry Add 600006 4/9/2012 PARVD/12-13/0005
100001 Ren/100001 Voucher Entry Add 600006 4/9/2012 PARVD/12-13/0006
100002 Ren/100002 Voucher Entry Add 600006 4/9/2012 PARVD/12-13/0007

about 78887rows,I want to upload it directly from text file to database,by the programming of c#

推荐答案

嘿,
的编程将其直接从文本文件上传到数据库.
有两种方法可以识别数据并将其拆分为文本文件中的列.
1.分隔列列表:创建数据时,将使用特定字符作为列分隔符.最常用的定界符是逗号('','')和制表符(''\ t'').
2.固定长度的列列表:每列定义为具有固定的列大小(通常是预定义的).如果列数据短于列宽,则用空格填充以获取特定大小.重要的是要注意,如果列大小与指定的列大小不同,则该行将被视为无效.

通过查看您的数据,在我看来第二种方法是适用的.

如果您可以更改文本文件的数据格式,则建议您采用第一种方法,因为它可以与Excel之类的应用程序一起查看,可以与使用T-SQL输入到MS SQL Server数据库中兼容,并且易于从代码中读取.好吧.此外,数据可以具有动态长度.

CSV格式:阅读 [ CP文章 [使用T-SQL的CSV输入 [
Hey there,

There are two ways of identifying and splitting data into columns in a text file.
1. Delimited column list: When the data is created, a specific character is used as the column delimiter. The most commonly used delimiters are comma ('','') and tab (''\t'').
2. Fixed length column list: Each column is defined to have a fixed column size (often pre-defined). If the column data is shorter than the column width, it''s padded with spaces to obtain the specific size. It''s important to note that if a column size is different to the specified column size, the row will be considered invalid.

By looking at your data, it seems to me as if the second method is applicable.

If you can change the data format of the text file, I suggest you go with the first method since it is compatible for viewing with applications like Excel, compatible for input using T-SQL into MS SQL Server databases and easy to read from code as well. Also, the data can be of dynamic lengths.

CSV Format: Read[^]
Using CSV in C#: CP Articles[^]
Input CSV file directly to database: CSV input using T-SQL[^]

Hope this helps, regards


要通过C#应用程序插入大量行,应使用使用SqlBulkCopy传输数据 [
For inserting large number of rows through C# application you should use SqlBulkCopy[^] (if you''re using MS SQL DB server).

Here''s an article to get you started:
Transferring Data Using SqlBulkCopy[^]


使用以下代码,会在具有五列的表中获取数据:

Use the following code, you''ll get the data in a table with five columns:

string str = File.ReadAllText(path_of_the _text_file);

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("Ren", typeof(string));
            dt.Columns.Add("Voucher", typeof(string));
            dt.Columns.Add("Date", typeof(string));
            dt.Columns.Add("Description", typeof(string));

            string[] text = File.ReadAllLines(textBox1.Text);
            DataRow row;

            foreach (string s in text)
            {
                row = dt.NewRow();
                string[] line = s.Split(' ');

                row["Id"] = line[0];
                row["Ren"] = line[1];
                row["Voucher"] = line[2] + " " + line[3] + " " + " " + line[4] + " " + line[5];
                row["Date"] = line[6];
                row["Description"] = line[7];

                dt.Rows.Add(row);
            }

            dataGridView1.DataSource = dt;


这篇关于将数据从文本文件导出到数据库.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆