ADO .Net – Dynamic Retrieval of Record

using System;
using System.Data.SqlClient;
namespace ADO1
{
            internal 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 to Database”);
 
 
                                                Console.WriteLine(“Enter student number to delete : “);
                                                int num = Convert.ToInt32(Console.ReadLine());
 
                                                String query = “select * from student where num=@num”;
                                                SqlCommand cmd = new SqlCommand(query, con);
 
                                                cmd.Parameters.AddWithValue(“@num”, num);
                                                SqlDataReader reader = cmd.ExecuteReader();
 
                                                if(reader.Read())
                                                {
                                                            Console.WriteLine(“Details are : “);
                                                            Console.WriteLine(reader.GetValue(0) + ” ,” + reader.GetValue(1));
                                                }
                                                else
                                                {
                                                            Console.WriteLine(“No such record to delete”);
                                                }
                                    }
                                    catch (Exception e)
                                    {
                                                Console.WriteLine(“Exception : ” + e.ToString());
                                    }
                                    finally
                                    {
                                                if(con != null)
                                                {
                                                            con.Close();
                                                            Console.WriteLine(“Closed”);
                                                }
                                    }
                        }
            }
}
Scroll to Top