读取文本文件并存储在数据库中 [英] Read the text file And store in Database

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

问题描述

我有一个文本文件,我想读取数据并存储在db中。在文本文件1到8中,coloumn是数据库中的一行,接下来的1到8列是数据库中的下一行,我想在db分隔逗号,

I am having a text file and i want to read the data and store in db . in text file 1 to 8 coloumn are one row in database and next 1 to 8 columns are next row in database and i wanto store in db separation of comma ,

1,280 ,  MacdonaldsEgg McMuffin,
2,250 ,MacdonaldsChicken Sausage McMuffin
3,225 ,MacdonaldsEgg  Cheese McMuffin,
4,180 ,MacdonaldsHash Browns (Single),
5,75 ,Macdonalds
6,2 pcs Hot Cakes,
7,240 ,Macdonalds
8,3 pcs Hot Cakes,
1,281,  MacdonaldsEgg McMuffin,
2,250 ,MacdonaldsChicken Sausage McMuffin
3,225 ,MacdonaldsEgg  Cheese McMuffin,
4,180 ,MacdonaldsHash Browns (Single),
5,75 ,Macdonalds
6,2 pcs Hot Cakes,
7,240 ,Macdonalds
8,3 pcs Hot Cakes,

推荐答案

List<string> lines = System.IO.File.ReadAllLines(@"somefile.txt").ToList();
var firstEightLines = lines.Take(8);
var secondEightLines = lines.Skip(8).Take(8);

foreach(var line in firstEightLines)
{
    var content = line.Split(',');
    var col1 = content[0];
    var col2 = content[1];
    var col3 = content[2];

    // store values in database
}
// same for the secondEightLines



没有经过测试,只是一个疯狂的猜测。但它应该工作。 :)



-KR


Haven't tested, just a wild guess. But it should work. :)

-KR


我想一种方法是使用MSDN:File.ReadLines [ ^ ]



此方法返回一个IEnumerable< string>所以这只是循环返回的行。



I guess one way to do it is to use MSDN: File.ReadLines[^]

This method returns a an IEnumerable<string> so then it is just a matter of looping through the returned lines.

foreach (string line in File.ReadLines(path))
{
    string[] columns = line.Split(',');
    // Do something with the columns
    // ...
}


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

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