954,219 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read certain # of lines

I have a program that reads an sql file and then executes it into the mysql database. The problem is that when a file larger than 1 mb is executed, an error comes up saying the file is too large. Is there anyway that i can read, say 200 lines of the file, and execute that?

Any help would be appreciated

SiPex

SiPexTBC
Light Poster
28 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Try using StreamReader.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ReadLines
{
    class Program
    {


        static void Main(string[] args)
        {

            using (StreamWriter _SW = new StreamWriter("File.txt"))
            {
                for (int i = 0; i < 200; i++)
                    _SW.WriteLine("Pre-200 #: " + i);
                for (int i = 0; i < 50; i++)
                    _SW.WriteLine("Post-200 #: " + i);
            }

            using (StreamReader _SR = new StreamReader("File.txt", Encoding.UTF8))
            {
                for(int i = 0; i < 200; i++)
                    Console.WriteLine(_SR.ReadLine());
            }

            Console.WriteLine("\n\n\nPress any key to exit");
            Console.ReadLine();
        }
    }
}
mariocatch
Junior Poster
103 posts since Apr 2007
Reputation Points: 11
Solved Threads: 17
 

Hrm.. i used that exact code but, i get a text file full of Pre-200 #: 1 continuously Any other suggestions?

SiPexTBC
Light Poster
28 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Then you aren't using the exact same code. Because I get the correct output.

mariocatch
Junior Poster
103 posts since Apr 2007
Reputation Points: 11
Solved Threads: 17
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You