ADO .Net – Delete Record in SQL Server Table

Delete Record in SQL Server Table:

  • We can invoke ExecuteNonQuery() method to perform all DML operations such as
    • Insertion of Record
    • Deletion of Record
    • Updation of Record
using System;
using System.Data.SqlClient;
namespace Practice
{
            class Program
            {
                        static void Main(string[] args)
                        {
                                    SqlConnection con = null;
                                    try
                                    {
                                                con = new SqlConnection(“data source=.; database=fullstack; integrated security=SSPI”);
                                                con.Open();
                                                Console.WriteLine(“Connected”);
 
                                                String query = “delete from account where num=102”;
                                                SqlCommand cm = new SqlCommand(query, con);
                                                cm.ExecuteNonQuery();
                                                Console.WriteLine(“Record Deleted Successfully”);
                                    }
                                    catch (Exception e)
                                    {
                                                Console.WriteLine(“OOPs, something went wrong.” + e);
                                    }
                                    finally
                                    {
                                                con.Close();
                                                Console.WriteLine(“Connection closed”);
                                    }
                        }
            }
}
Scroll to Top