Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello every body

iam developing windows project using c#. i have to import text file records into sqlserver 2005 db.
My question is
i want to read records from text file usig student idno.

i have an idea of read exact value from text file using strings that is finding student no:1001 value .
i want to read every student record based on particular student no? not for 1001

so iwant standered code to read particular records based on student nuber as
my choice.
plz help me.......

my text file like this
  |-----------------------------------------------|
  |            student no:1001                             |
  |           address:ongole,A.P,                 |
  |           hyderabad,india.                    |
  | |
  |  stdid stdname   class                        |
  |   1     raheem   mca                          |
  |   2     sudheer  mca                          |
  | ----------------------------------------------|
  | student no:1002                               |
  |   address:ongole,A.P,                         |
  |           hyderabad,india.                    |
 -|
  |  stdid stdname   class                        |
  |   1     raheem   mca                          |
  |   2     sudheer  mca                          |
 --------------------------------------------------
|


My code is

C#
private void BtnImpstrm_Click(object sender, EventArgs e)
       {
           openFileDialog1.ShowDialog();
           string f = openFileDialog1.FileName;
           StreamReader sr = new StreamReader(f);
           string s = sr.ReadToEnd();
           MessageBox.Show(s);
           sr.Close();
           str =s.Split('\n');
           for (int i = 0; i < str.Length; i++)
           {
               if (str[i].ToString().Trim().ToUpper().Contains("Student NO :  1001"))
               {
                   string[] words = str[i].Split(' ');
                   string output = words[word no].ToString().Trim();
                   string rec1 =str[line no].ToString().Trim();
                   string rec2 = str[line no].ToString().Trim();
                   MessageBox.Show(output);
                   MessageBox.Show(rec1);
                   MessageBox.Show(rec2);

               }
           }

       }
Posted
Updated 11-Sep-11 21:20pm
v7
Comments
Sergey Alexandrovich Kryukov 12-Sep-11 2:25am    
Why text? A text in such a free format, ad-hoc and potentially unreliable?
--SA
Pravin Patil, Mumbai 12-Sep-11 2:59am    
as SA has said it it not reliable to use plain vanila text file, use SQL/Oracle instead. They are proven. Else you can also think of Fixed Length records in text file as well.

1 solution

Like the two previous responders, I'm not sure as to your choice of a flat text file, but I have to ask, even when you chose a flat file, why didn't you enforce some sort of structure?

In this particular case, the only way you can do it is to sequentially read through every line (they don't even qualify as records). This is extremely time consuming especially for larger files.

If you had enforced some structure then you could have done Random Access (System.IO.FileStream.Seek()).

Of course, XML is even better at this.

In every case, I would suggest you reconsider the use of text files for this scenario.
 
Share this answer
 

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