Saturday, June 10, 2023

Database ConsoleApp Lab 1

 using System;


using System.Data.SqlClient;


namespace ConsoleApp1

{

    class ProgramDb

    {

        static void Main(string[] args)

        {


            string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=SampleDb;Integrated Security=SSPI";

            

            //Create the connection object

            SqlConnection con = new SqlConnection(connString);


            try {

                SqlCommand cm = new SqlCommand("select * from Student", con);

                con.Open();


                Console.WriteLine("Connection Successfully");

                SqlDataReader sdr = cm.ExecuteReader();

                while (sdr.Read())

                {

                    Console.WriteLine(sdr["name"]);

                }

                Console.ReadKey();


            }

            catch (Exception e) {

                Console.WriteLine(e);

            }

            finally

            {

                con.Close();


            }



        }

    }

}



No comments:

Post a Comment

Lab 3 Unit -5 Database

   private void button1_Click(object sender, EventArgs e)         {             ProductDbEntities productDb = new ProductDbEntities();      ...