Explain Data reader object and use.
In : BSc IT Subject : Web Application Development - ASP.NETA Data Reader is a fast, read-only, forward-only stream of data from a database.
Use:
- Retrieves data quickly and efficiently when you only need to read it once (e.g., displaying data in a list).
- Connected architecture: works while the connection to the database is open.
Example :
using (SqlConnection conn = new SqlConnection("your_connection_string")) {
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
Console.WriteLine(reader["CustomerName"]);
}
}