Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use ExecuteReader in ADO.NET on Windows

ExecuteReader is a method in ADO.NET, a part of the .NET Framework used for accessing databases. It is specifically used to execute SQL commands that return rows, such as SELECT statements. This method is applicable in the Windows environment when developing applications that interact with databases like SQL Server.

Examples:

  1. Using ExecuteReader in a Console Application

    Here’s how you can use ExecuteReader in a C# console application to retrieve data from a SQL Server database:

    using System;
    using System.Data.SqlClient;
    
    class Program
    {
       static void Main()
       {
           string connectionString = "Data Source=your_server;Initial Catalog=your_database;Integrated Security=True";
           string queryString = "SELECT FirstName, LastName FROM Employees";
    
           using (SqlConnection connection = new SqlConnection(connectionString))
           {
               SqlCommand command = new SqlCommand(queryString, connection);
               connection.Open();
               SqlDataReader reader = command.ExecuteReader();
    
               while (reader.Read())
               {
                   Console.WriteLine($"{reader["FirstName"]} {reader["LastName"]}");
               }
               reader.Close();
           }
       }
    }

    In this example, replace your_server and your_database with your actual SQL Server name and database name. The program connects to the database, executes a SELECT query using ExecuteReader, and prints out the first and last names of employees.

  2. Using ExecuteReader in a Windows Forms Application

    If you are developing a Windows Forms application, you can use ExecuteReader to populate a DataGridView with data from a database:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    public partial class Form1 : Form
    {
       public Form1()
       {
           InitializeComponent();
           LoadData();
       }
    
       private void LoadData()
       {
           string connectionString = "Data Source=your_server;Initial Catalog=your_database;Integrated Security=True";
           string queryString = "SELECT FirstName, LastName FROM Employees";
    
           using (SqlConnection connection = new SqlConnection(connectionString))
           {
               SqlCommand command = new SqlCommand(queryString, connection);
               connection.Open();
               SqlDataReader reader = command.ExecuteReader();
    
               DataTable dataTable = new DataTable();
               dataTable.Load(reader);
               dataGridView1.DataSource = dataTable;
    
               reader.Close();
           }
       }
    }

    This code connects to a SQL Server database, retrieves data using ExecuteReader, and loads it into a DataTable, which is then used as the DataSource for a DataGridView control on the form.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.