Click here to Skip to main content
14,599,199 members
Rate this:
Please Sign up or sign in to 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
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();
}
Khadar Babu Salijamala 17-Oct-15 10:25am
   
please help me
Khadar Babu Salijamala 17-Oct-15 9:24am
   
it showing only first line
Krunal Rohit 18-Oct-15 2:09am
   
Post your whole code & let's see.

-KR
Khadar Babu Salijamala 18-Oct-15 5:27am
   
its working....but i have another question...how to insert by using entity frame work . my code is
using (sampleDbEntities sd = new sampleDbEntities())
{
var t = new AccountDetails
{
Product_Name = d.Product_Name.ToString(),
Product_Name1 = d.Product_Name1,
Product_Name2 = d.Product_Name2,
Product_Name3 = d.Product_Name3,
Product_Name4 = d.Product_Name4,
Product_Name5 = d.Product_Name5,
Product_Name6 = d.Product_Name6,
Product_Name7 = d.Product_Name7,
Product_Price = d.Product_Price,
Product_Price1 = d.Product_Price1,
Product_Price4 = d.Product_Price4,
Product_Price2 = d.Product_Price1,
Product_Price3 = d.Product_Price1,
Product_Price5 = d.Product_Price5,
Product_Price6 = d.Product_Price6,
Product_Price7 = d.Product_Price7,
};

sd.Products.Add(t);
sd.SaveChanges();


its getting error in sd.products.add(t);
showing error is best overloadedmethod must match
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
    // ...
}
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100