Reputation: 21
I have this code
string query = "SELECT * FROM table;"
try
{
using (SqlConnection connection = new SqlConnection(this.ConnectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand(query, connection))
{
SqlDataReader reader = cmd.ExecuteReader();
}
connection.Close();
}
}
In reader I receive the number of rows in the table, but no data. Where am I getting lost?
Upvotes: 0
Views: 272
Reputation: 16
You need to read the reader
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//
}
}
Upvotes: 0
Reputation: 62472
Once you've called ExecuteReader
you need to loop through it, reading each row:
while(reader.Read())
{
// Process the row...
}
Also, it's good practice to put the reader into a using
block:
using(SqlDataReader reader = cmd.ExecuteReader())
{
while(reader.Read())
{
// Process the row
}
}
Upvotes: 3