从文本文件中读取并将其移动到数据库 [英] reading from text file and move it to database

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

问题描述

嗨bax

i希望逐行从文本文件中读取数据并将它们移动到数据库

我该怎么办呢请帮助我





示例数据:

 1000次
2000分钟
2000秒
3000天
4000周


数字和单词之间的
是一个空格



[/ EDIT]

解决方案

首先应创建一个可存储的数据来存储数据。然后你应该:

  1. 解析文本文件
  2. 将数据插入表中



您可以使用以下方法完成任务逐行方法:也就是说,对于输入文本的每一行,首先解析行本身,然后将提取的数据写入表的新记录。



解析这条线很简单:只需使用 string.split [ ^ ]方法(如果合适,使用 Int32 解析方法 [ ^ ])。



写入表格应该是也很简单:查看其中一个许多教程 [ ^ ]可用。


您可以使用OleDb对象读取文本文件...



看看我的p revious answer:读取文本文件特定列 [ ^ ]。虽然,解决方案是在VB.NET中,但您可以使用 VB.Net到C#转换器 [ ^ ]。



为了帮助您解决第二部分问题,您需要提供更多详细信息。请参阅CPallini的解决方案。


虽然这个答案有很多出错的地方,但这是一个简单的解决方案。



 使用 System.Data.Linq; 
使用 System.Data.Linq.Mapping;

命名空间 FileToDb
{
[数据库]
public class MyDataDB:DataContext
{
public MyDataDB() : base 数据源= ...);

public 表< MyData>迈德特;
}

[表格(名称= MyData) ]
public class MyData
{
[Column] public int 编号{ set ; get ; }
[列] public string 名称{设置; get ; }
}

public class 计划
{
public void Main()
{
var dbContext = new MyDataDB();
var lines = File.ReadAllLines( 路径);
foreach var line in 行)
{
var tokens = line.Split( new char [] {' '},StringSplitOptions。 RemoveEmptyEntries);
var data = new MyData();
data.Number = int .Parse(tokens [ 0 ]);
data.Name = tokens [ 1 ];
dbContext.MyData.InsertOnSubmit(data);
}
dbContext.SubmitChanges();
}
}

}





没有测试,因为我'我需要设置一个数据库。

我确实遗漏了验证码。你不应该! :-)



我非常依赖Abby Fichtner(Hacker Chick)的LINQ教程( 1 2


hi bax
i want reading data from a text file line by line and move them to data base
how can i do it please help me

[EDIT - moved from comments]
Example data:

1000 time
2000 min
2000 second
3000 day
4000 week


between number and word is one space

[/EDIT]

解决方案

You should first create a sutable table to store your data in. Then you should:

  1. Parse your text file
  2. insert data into the table


You may accomplish the task with a line-by-line approach: that is, for each line of the input text, you first parse the line itself and then write the extracted data to a new record of the table.

Parsing the line is easy: just use string.split[^] method (then, if appropriate, use Int32 parsing methods see[^]).

Writing to the table should be easy too: check out one of the many many tutorials[^] available.


You can read text files using OleDb objects...

Have a look at my previous answer: Read Text File Specific Columns[^]. Although, the solution is in VB.NET, but you can use VB.Net to C# converter[^].

To help you with the second part of question, you need to provide more details. See CPallini''s solution.


Although there is a lot of room for going wrong in this answer, here is a simple solution.

using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace FileToDb
{
    [Database]
    public class MyDataDB : DataContext
    {
        public MyDataDB ( ) : base("Data Source=..."  );

        public Table<MyData> MyData;
    }

    [Table( Name = "MyData" )]
    public class MyData
    {
        [Column] public int Number { set; get; }
        [Column] public string Name { set; get; }
    }

    public class Program
    {
        public void Main()
        {
            var dbContext = new MyDataDB();
            var lines = File.ReadAllLines("path");
            foreach (var line in lines)
            {
                var tokens = line.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
                var data = new MyData();
                data.Number = int.Parse(tokens[0]);
                data.Name = tokens[1];
                dbContext.MyData.InsertOnSubmit(data);
            }
            dbContext.SubmitChanges();
        }
    }

}



didn''t test, because I''d need to set up a DB.
and I did leave out validation code. you shouldn''t! :-)

I heavily relied on LINQ tutorials by Abby Fichtner (Hacker Chick) (1 and 2


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

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