Articles → ADO.NET → Datareader In Ado.Net

Datareader In Ado.Net






Purpose





Example


using System;
using System.Data.SqlClient;

namespace ReaderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = @"[connection_string]";
            string query = "select * from Courses";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine(reader["course_name"]);
                    }
                }
            }            
            Console.ReadLine();
        }
    }
}



Output


Picture showing reading the data using datareader



Posted By  -  Karan Gupta
 
Posted On  -  Friday, May 10, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250