SqlCommand Class ADO.Net | ExecuteNonQuery | ExecuteReader | ExecuteScalar

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;


namespace SQLConnectionADO

{

    internal class Program

    {

        static void Main(string[] args)

        {

            Program.Connection();

            Console.ReadLine();

        }

        static void Connection()

        {

            string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

            SqlConnection con = null;

            try

            {

                using (con = new SqlConnection(cs))

                {

                    string query = "select * from employee_tbl";

                    SqlCommand cmd = new SqlCommand(query,con);

                    con.Open();

                    SqlDataReader dr = cmd.ExecuteReader();

                    while(dr.Read())

                    {

                        Console.WriteLine("Id = " + dr["id"] + ", Name = " + dr["name"] + ", Gender = " + dr["gender"] + ", Age = " + dr["age"] + ", Salary = " + dr["salary"] + ", City = " + dr["city"]);

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            finally

            {

                con.Close();

            }

        }



        //static void Connection()

        //{

        //    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

        //    SqlConnection con = null;

        //    try

        //    {

        //        using (con = new SqlConnection(cs))

        //        {

        //            con.Open();

        //            if (con.State == ConnectionState.Open)

        //            {

        //                Console.WriteLine("Connection has been Created Successfully !!");

        //            }

        //        }

        //    }

        //    catch (SqlException ex)

        //    {

        //        Console.WriteLine(ex.Message);

        //    }

        //    finally

        //    {

        //        con.Close();

        //    }

        //}



        //static void Connection()

        //{

        //    string cs = "Data Source=HEER\\SQLEXPRESS;Initial Catalog=ado_d;Integrated Security=true;";

        //    SqlConnection con = null;

        //    try

        //    {

        //        using (con = new SqlConnection(cs))

        //        {

        //            con.Open();

        //            if (con.State == ConnectionState.Open)

        //            {

        //                Console.WriteLine("Connection has been Created Successfully !!");

        //            }

        //        }

        //    }

        //    catch (SqlException ex)

        //    {

        //        Console.WriteLine(ex.Message);

        //    }

        //    finally

        //    {

        //        con.Close();

        //    }



        //    //using(SqlConnection con = new SqlConnection(cs))

        //    //{

        //    //    con.Open();

        //    //    if (con.State == ConnectionState.Open)

        //    //    {

        //    //        Console.WriteLine("Connection has been created successfully.");

        //    //    }

        //    //}



        //    //SqlConnection con = new SqlConnection(cs);

        //    //try

        //    //{

        //    //    con.Open();

        //    //    if (con.State == ConnectionState.Open)

        //    //    {

        //    //        Console.WriteLine("Connection has been created successfully.");

        //    //    }

        //    //}

        //    //catch (SqlException ex)

        //    //{

        //    //    Console.WriteLine(ex.Message);

        //    //}

        //    //finally

        //    //{

        //    //    con.Close();

        //    //}

        //}

    }

}

Above File is SQLConnectionADO\Program.cs








<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
<connectionStrings>
<!--<add name="dbcs" connectionString="Data Source=HEER\\SQLEXPRESS;initial Catalog=ado_db;Integrated Security=true;" providerName="System.Data.SqlClient"/>-->
<add name="dbcs" connectionString="Data Source=.\SQLEXPRESS;initial Catalog=ado_db;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Above File is SQLConnectionADO\App.config
















Comments

Popular posts from this blog

Create a User Registration Form in ASP.NET using SQL Server, Visual Studio 2022 & Bootstrap

Create a Simple Login Form in ASP.NET using Visual Studio 2022