Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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]
Posted
Updated 4-Mar-13 2:03am
v2
Comments
Maciej Los 4-Mar-13 7:46am    
Example please, example!!!
Does file is comma, tab (or any) separated?
fatima...68 4-Mar-13 7:50am    
thanks ok
like this:
1000 time
2000 min
2000 second
3000 day
4000 week
Maciej Los 4-Mar-13 8:06am    
Next time, use "Improve question" button.
Does this file contains headers?
Do you want to save data into MS Access, MS SQL Server database?
fatima...68 4-Mar-13 7:50am    
between number and word is one space
OriginalGriff 4-Mar-13 7:57am    
That's not really enough detail - what do you want to do with it? What DB are you using? What fields do you have in your table(s)? and so forth.
Use the "Improve question" widget to edit your question and provide better information.

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.
 
Share this answer
 
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.
 
Share this answer
 
Although there is a lot of room for going wrong in this answer, here is a simple solution.

C#
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
 
Share this answer
 
Comments
fatima...68 5-Mar-13 0:04am    
thanks my friend

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