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 ExecuteNonQuery in a Windows Environment

ExecuteNonQuery is a method commonly used in ADO.NET, a data access technology from the Microsoft .NET Framework. This method is essential for executing SQL statements that do not return any data, such as INSERT, UPDATE, DELETE, and DDL commands. Understanding how to use ExecuteNonQuery is crucial for developers working with databases in a Windows environment, as it allows for efficient data manipulation and management within applications.

In a Windows environment, ExecuteNonQuery can be used within .NET applications, typically written in languages such as C# or VB.NET. This article will provide practical examples of how to use ExecuteNonQuery in a Windows environment using C# and the .NET Framework.

Examples:

  1. Basic Example of ExecuteNonQuery in C#:

    using System;
    using System.Data.SqlClient;
    
    class Program
    {
       static void Main()
       {
           string connectionString = "your_connection_string_here";
           string sqlQuery = "INSERT INTO Employees (Name, Position) VALUES ('John Doe', 'Developer')";
    
           using (SqlConnection connection = new SqlConnection(connectionString))
           {
               SqlCommand command = new SqlCommand(sqlQuery, connection);
               connection.Open();
               int rowsAffected = command.ExecuteNonQuery();
               Console.WriteLine($"Rows affected: {rowsAffected}");
           }
       }
    }
  2. Using ExecuteNonQuery with Parameters:

    using System;
    using System.Data.SqlClient;
    
    class Program
    {
       static void Main()
       {
           string connectionString = "your_connection_string_here";
           string sqlQuery = "UPDATE Employees SET Position = @Position WHERE Name = @Name";
    
           using (SqlConnection connection = new SqlConnection(connectionString))
           {
               SqlCommand command = new SqlCommand(sqlQuery, connection);
               command.Parameters.AddWithValue("@Position", "Senior Developer");
               command.Parameters.AddWithValue("@Name", "John Doe");
    
               connection.Open();
               int rowsAffected = command.ExecuteNonQuery();
               Console.WriteLine($"Rows affected: {rowsAffected}");
           }
       }
    }
  3. Handling Exceptions:

    using System;
    using System.Data.SqlClient;
    
    class Program
    {
       static void Main()
       {
           string connectionString = "your_connection_string_here";
           string sqlQuery = "DELETE FROM Employees WHERE Name = 'John Doe'";
    
           try
           {
               using (SqlConnection connection = new SqlConnection(connectionString))
               {
                   SqlCommand command = new SqlCommand(sqlQuery, connection);
                   connection.Open();
                   int rowsAffected = command.ExecuteNonQuery();
                   Console.WriteLine($"Rows affected: {rowsAffected}");
               }
           }
           catch (SqlException ex)
           {
               Console.WriteLine("An error occurred while executing the SQL query.");
               Console.WriteLine($"Error: {ex.Message}");
           }
       }
    }

These examples demonstrate how to use ExecuteNonQuery in a Windows environment using C#. By following these examples, developers can effectively execute SQL commands that modify data without returning any results.

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.