Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
hi, 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,
Posted
Updated 17-Oct-15 4:03am
Comments
Khadar Babu Salijamala 17-Oct-15 7:12am    
when i reading the data from console application . it reads all the data. but i want to read first 8 columns and second time next 8 columns in note pad

C#
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

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

-KR
 
Share this answer
 
v3
Comments
Khadar Babu Salijamala 17-Oct-15 8:25am    
its not working...thanks for your reply
Krunal Rohit 17-Oct-15 8:29am    
What's the error ?

-KR
Khadar Babu Salijamala 17-Oct-15 10:05am    
Results View = Expanding the Results View will enumerate the IEnumerable

it was the error
Khadar Babu Salijamala 17-Oct-15 10:24am    
its not taking second 8 lines and also its taking first 7 lines only
Khadar Babu Salijamala 17-Oct-15 10:24am    
static void Main(string[] args)
{
List<string> lines = System.IO.File.ReadAllLines(@"C:\khadar\Demo.mnk").ToList();
var firstEightLines = lines.Take(7).ToList();
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];
// var col4 = content[3];
//var col5 = content[4];
//var col6 = content[5];
//var col7 = content[6];
//var col8 = content[7];
// var3 = content[2];
Console.WriteLine(line);
Console.ReadLine();
}
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.

C#
foreach (string line in File.ReadLines(path))
{
    string[] columns = line.Split(',');
    // Do something with the columns
    // ...
}
 
Share this answer
 
v3
Comments
Khadar Babu Salijamala 17-Oct-15 9:36am    
its not working.. it showing all the columns, i want only 8 columns in the text file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900