Reputation: 35
1) I'm trying to read a text file but it shows error Like
foreach statement cannot operate on variables of type 'System.IO.StreamReader' because 'System.IO.StreamReader' does not contain a public definition for 'GetEnumerator'
Here is my C# code...
using (StreamReader sr = new StreamReader(@"D:\test1.txt"))
{
while (sr.Peek() >= 0)
{
//contents of foreach loop go here
foreach (var line in sr)
{
var items = line.Split(new[] { '\t', '\n' }).ToArray();
if (items.Length != 3)
continue;
var Name = items[0].ToString();
var Email = items[1].ToString();
var Pwd = items[2].ToString();
cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
Upvotes: 1
Views: 2023
Reputation: 21969
Basically, what Alex.K comment say:
using (StreamReader sr = new StreamReader(@"D:\test1.txt"))
{
while (sr.Peek() >= 0)
{
var line = sr.ReadLine();
var items = line.Split(new[] { '\t', '\n' }).ToArray();
if (items.Length != 3)
continue;
var Name = items[0].ToString();
var Email = items[1].ToString();
var Pwd = items[2].ToString();
cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
You don't need foreach
inside peek check loop, just read a line.
Here is a typical use from MSDN, which is very clear of how to read lines:
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Console.WriteLine(sr.ReadLine());
}
}
To avoid famous SQL injection vulnerability
, deal with parameters using Parameters
:
// this is baaaaad
cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";
// will become
cmd.CommandText = "insert into Employees values(@Name, @Email, @Pwd)";
cmd.Parameters["@Name"].Value = Name;
cmd.Parameters["@Email"].Value = Email;
cmd.Parameters["@Pwd"].Value = Pwd;
// somewhere before, where you create command
cmd.Parameters.Add("@Name", type);
cmd.Parameters.Add("@Email", type);
cmd.Parameters.Add("@Pwd", type);
Upvotes: 1
Reputation: 4021
This is going to work for this. Instead of ForEach
Loop, use a while
loop
using (StreamReader sr = new StreamReader(@"D:\test1.txt"))
{
while (sr.Peek() >= 0)
{
//contents of foreach loop go here
while ((line = sr.ReadLine()) != null)
{
var items = line.Split(new[] { '\t', '\n' }).ToArray();
if (items.Length != 3)
continue;
var Name = items[0].ToString();
var Email = items[1].ToString();
var Pwd = items[2].ToString();
cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
Upvotes: 1
Reputation: 2024
you can use for in this type
foreach (var line in File.ReadLines(@"D:\test1.txt"))
{
}
Upvotes: 0
Reputation: 12196
The error is pretty clear.
You can only iterate over via foreach
on elements that implements the GetEnumerator
method, and StreamReader
isn't.
There are plenty of excellent and good example for StreamReader
just look one up.
Upvotes: 1