ADO .Net – Insert Record into SQL Server

Insert record into table:

  • In this example, we specify the record values directly in SQL Statement.
  • Dynamic insertion of record will see in the coming concepts.
using System;
using System.Data.SqlClient;
namespace Practice
{
            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”);
 
                                                String query = “insert into account values(101, ‘Amar’, 5000)”;
                                                SqlCommand cm = new SqlCommand(query, con);
                                                cm.ExecuteNonQuery();
                                                Console.WriteLine(“Record inserted Successfully”);
                                    }
                                    catch (Exception e)
                                    {
                                                Console.WriteLine(“OOPs, something went wrong.” + e);
                                    }
                                    finally
                                    {
                                                con.Close();
                                                Console.WriteLine(“Connection closed”);
                                    }
                        }
            }
}
Scroll to Top