This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

13 Nov 2012

Simple C# program with ADO.NET

In this post i am posting a simple c# console program to start with ADO.NET. This may help the beginners in understanding the insertion of data into a database from a c# program.

Before going for the program create a table in sql server using the following command:

CREATE TABLE EMP_DETAILS(EMP_ID INT,EMP_NAME VARCHAR(25))


Now i am writing a c#  program to insert data into the table EMP_DETAILS..



using System;
using System.Data.SqlClient; //Namespace for connecting to sql server

namespace First_DataBase_Program
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            //if u r using windows authentication for sql server then ConnectionString is
            con.ConnectionString = "Data Source=LocalHost;Integrated Security=true;Initial                                       Catalog=sameer";
            /*if u r using sql authenticaton for sql server then ConectionString is
            con.ConnectionString = "Data Source=LocalHost;Initial Catalog=<database                        name>;user id=<user name>; password=<password>";
            */

            int Emp_Id = 0;
            string Emp_Name = "", option = "Y";
            Console.WriteLine(">> Simple C# Console Program to Start with ADO.NET<<");
            try
            {
                do
                {
                    if (option != "Y" && option != "y" && option != "N" && option != "n")
                    {
                        Console.Write("Press Y to continue and N to exit : ");
                        option = Console.ReadLine();
                    }
                    else
                    {
                        Console.Write("\n Enter employee id:");
                        Emp_Id = Convert.ToInt32(Console.ReadLine());
                        Console.Write("\n Enter Employee Name:");
                        Emp_Name = Console.ReadLine();
                        cmd.CommandText = "INSERT INTO Emp_Details VALUES(" + Emp_Id + ",'" +                                             Emp_Name + "')";
                        cmd.Connection = con;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        Console.WriteLine("\n Command is:" + cmd.CommandText + "\n Record                                                Inserted successfully");
                    }
                    Console.Write("\n Press Y to continue and N to exit : ");
                    option = Console.ReadLine();
                } while (option != "N" || option == "n");

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                //Console.WriteLine("\n Error Occured...Try Again");
                Console.Write(ex.Message);
                Console.ReadLine();
            }

        }

    }

}

}

Execute the program to insert the records in the table EMP_DETAILS. The sample output is given below.



Now open the sql server and type the following command and execute ..

SELECT * FROM EMP_DETAILS


Then u can see the inserted records in the table EMP_DETAILS as shown below.