ADO.NET SqlCommand Class in C#

ADO.NET SqlCommand Class in C#

 

using System;  
using System.Data.SqlClient;  
namespace usingcsharp
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            new Program().CreateTable();  
        }  
        public void CreateTable()  
        {  
            SqlConnection con = null;  
            try  
            {  
                // Create Connection  
                con = new SqlConnection("data source=.; database=test; integrated security=True");  
                // write sql query  
                SqlCommand cm = new SqlCommand("select * from tbl_test", con);  
                // Open Connection  
                con.Open();  
                // Executing the SQL query  
                SqlDataReader dr = cm.ExecuteReader();  
                while (dr.Read())  
                {  
                    Console.WriteLine(dr["Name"]+"-"+ dr["MobilNo"]);  
                }  
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine("Something went wrong." + ex);  
            }  
            // Close the connection  
            finally  
            {  
                con.Close();  
            }  
        }  
    }  
}

2021-10-03