ADO - SQL Server - Delete Record

using System;
using System.Data.SqlClient;
class Program
{
    static void Main()
    {
        string connectionString = "Data Source=(local);Initial Catalog=YourDatabaseName;Integrated Security=True";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            try
            {
                connection.Open();
                // Connection opened successfully
                Console.WriteLine("Connected to the database.");
                // Delete a record
                string deleteQuery = "DELETE FROM YourTableName WHERE Id = @Id";
                SqlCommand deleteCommand = new SqlCommand(deleteQuery, connection);
                // Set the parameter value
                deleteCommand.Parameters.AddWithValue("@Id", 1);
                int rowsAffected = deleteCommand.ExecuteNonQuery();
                Console.WriteLine($"Record deleted successfully. Rows affected: {rowsAffected}");
            }
            catch (Exception ex)
            {
                // Handle any errors
                Console.WriteLine("Error: " + ex.Message);
            }
        }
    }
}

In the above code, make sure to replace "YourDatabaseName" with the actual name of the database you want to connect to, "YourTableName" with the name of the table you want to delete the record from, and provide the appropriate value for the @Id parameter.

Here's an explanation of the code:

  • We start by defining the connection string, which includes the necessary details to connect to the SQL Server database. Update the "Data Source" to your server name or IP address, and "Initial Catalog" to your database name.
  • Inside the using block, we create a new SqlConnection object and pass the connection string as the constructor parameter. The using statement ensures that the connection is properly closed and disposed of when we're done with it.
  • We attempt to open the connection using the Open method. If the connection is successful, we print a message indicating that the connection has been established.
  • We define the SQL query to delete a record from the table in the deleteQuery variable. Replace "YourTableName" with the name of your table, and specify the condition for deleting the record. In this example, we are deleting the record with a specific ID.
  • We create a new SqlCommand object with the deleteQuery and the connection as parameters.
  • We set the parameter value using the Parameters.AddWithValue method of the SqlCommand object. Replace the parameter name and value (@Id and 1) with the appropriate values for the record you want to delete.
  • We execute the query using the ExecuteNonQuery method of the SqlCommand object. This method is used for executing queries that don't return any data. The ExecuteNonQuery method returns the number of rows affected by the query.
  • If the record deletion is successful, we print a message indicating that the record has been deleted, along with the number of rows affected.
  • If any errors occur during the process, they are caught in the catch block, and an error message is displayed.