简体   繁体   中英

Search string from text file and display selected line

Im new to C#, now rushing on a airTicket Booking System project. I have to search the booking number from a text file and display the booking detail.

This will be my record in the text file.

Booking Number :KW2MSMB30

Name :Testing

Age :21

Passport Num :Testing

Airline :Malaysia Airline

Depart from :malaysia

Return to :singapore

Departure date :Wednesday, 19 February 2014

Return date :Null

Travaller :1 (Adult) 1 (Children) 1 (Infant)

Service Class :Business Class

Trip: One Trip

After i key in the booking number and click on the OK button, it will show up the detail on a label.

This is what my code:

int counter = 0; string line;
        bool writeNextLine = false;
        StringBuilder sb = new StringBuilder();

        // Read the file and display it line by line.              
        using (System.IO.StreamReader file = new System.IO.StreamReader("record.txt"))
        {
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(txt_SearchBooking.Text))
                {
                    // This append the text and a newline into the StringBuilder buffer       
                    sb.AppendLine(line.ToString());

                    lbl_result.Text += sb.ToString();
                }

            }
        }

But this only show up my first line which is the booking number. what i should do to show up all the detail?

Sorry for my poor english, hope you guys understand this.

That is because the first line contains your booking number and the if clause will not help you fetch further details.

while ((line = file.ReadLine()) != null)
{
      if (line.Contains(txt_SearchBooking.Text)) //Will always match line Booking Number :KW2MSMB30
      {
            // This append the text and a newline into the StringBuilder buffer       
            sb.AppendLine(line.ToString());
            lbl_result.Text += sb.ToString();
      }
}

If you want to do is loop through the lines once you find the booking number and then break on next booking number.

    bool isBookingRecordFound = false;
    while ((line = file.ReadLine()) != null)
    {
          if (line.Contains(txt_SearchBooking.Text)) //Will always match line Booking Number :KW2MSMB30
          {
               isBookingRecordFound = true; //True here will ensure there was a record found
               sb.AppendLine(line.ToString());
               while ((line = file.ReadLine()) != null)
               {
                   if (line.Contains("Booking Number :"))
                       break;

                   sb.AppendLine(line.ToString());
               }
          }
          lbl_result.Text = sb.ToString();
    }

    if (!isBookingRecordFound)
       MessageBox.Show("There was no matching booking number");

IMHO, you should avoid the way you are using file to store data and access it as you are doing. Why not think of having SQL for the same?

IMHO, it's not about C# or SQL. If you are programming, first understand your requirements, then check whether you handle huge complex data, choose the right tool like SQL, learn up and start off. Unless this is just learning for C#, where you still can do it but not a preferred good process.

Update : For understanding more on usage of C# and SQL, there are many online articles. You can start with this: http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C

As a few others have said, you really should look into a database solution. C# can easily pull data from databases using LINQ. LINQ will return your database queries as an object which can be easily manipulated and displayed.

I would start here with LINQ. http://msdn.microsoft.com/en-us/library/bb397926.aspx

There are many resources available on how to get a simple DB going.

using Files for storing your data is not a good practice, rather i would suggest you to use DataBase for storing your data , because it would allow you to store it in a well efficient manner and also provides you SQL by which you can fetch the required information without much pain.

if your file contains only one booking info you can try the following code:

Try This:

StringBuilder sb = new StringBuilder();
if (File.ReadAllText("record.txt").Contains(txt_SearchBooking.Text))
 {
    String [] strLines=File.ReadAllLines("record.txt");
    foreach (String strLine in strLines)
    sb.AppendLine(strLine);
 }

I think there is not need that I tell you about Databases. So if we have a text file, let's work with it:

while ((line = file.ReadLine()) != null)
        {
            if (line.Contains(txt_SearchBooking.Text))
            {
                lbl_result = ReadBooking(file);
            }

        }

And then you add a new method:

string ReadBooking(StreamReader file) 
{
     StringBuilder sb = new StringBuilder();
     while ((line = file.ReadLine()) != null) || (line.Contains("Booking Number :"))
           {
               sb.AppendLine(line.ToString());

           } 
     return sb.ToString();        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM