Your while-loop overwrites on each iteration
str
with the last read
line
. So, when the loop is finished,
str
only contains the very last line of your file. You should instead append
line
to
str
and that's best done with a StringBuilder:
string line;
StringBuilder sb = new StringBuilder();
int counter = 0;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
counter++;
}
txtQuery.Text = sb.ToString();
Note that this all only makes sense if you actually need the line-count of the file. Otherwise you could just read it all at once with
sr.ReadToEnd()
Edit: You should use your
StreamReader
and your
SqlCommand
in a
using
-Block and supply the argument
CommandBehavior.CloseConnection
to your
ExecuteReader
-call.