C Sharp Program to Connect to Database-Insert Value in Database Table


As you know, The Application softwares have front end and back end.Many of the applications , windows or web requires Database to store records. The front end is the User Interface using which Clients or Users use the Applications and the Back end is the one who provides the data or stores the data. In the Web or Windows Applications coded in C# also has the back end associated to provide the data and information or to store the data. It is very necessary for a program to interact with the database. C# ADO.net provides the classes and methods to connect to database. Interacting with the database is very simple in C#. Let's have a look at below program for better understanding.

Below is the C Sharp Program to Connect to Database.

The Database used here is the Microsoft SQL Server 2014.


using System;

using System.Collections.Generic;

using System.Data.SqlClient;

namespace Connect_Databse

{

           class Program

          {

                   static void Main(string[] args)

                   {

                           string connectionstring = "data source=VISHAL-                                                                                     PC\\SQL2014;database=Test;uid=sa;password=ka123";

                           SqlConnection con = new SqlConnection(connectionstring);

                           con.Open();

                           Console.WriteLine("Connection opened");

                            Console.ReadKey();

                   }

            }

}



OutPut.

Connection opened.



Explanation

  • Using System- The System is the namespace containing all of the basic important classes and methods.
  • Using System.Data.Sqlclient- This namespace contains all the required classes to interact with the database.
  • Class Program- is the name of the class.
  • static void main(String [] args)- The main methods where the Console Program starts executing.
  • string connectionstring- is the variable to store the connection to Database.
  • Data Source- is the name of the Database Server.
  • Database- Name of the database to interact with.
  • uid- User id to connect to Database.
  • password- password required for authentication.
  • SqlConnection con=new SqlConnection(connectionstring)-Sqlconnection is the name of the class which is used to establish connection and interact with the Database. We passed the connectionstring to SqlConnection Constructor indication the Database path to connect with.
  • con.open()-Method of sqlconnection class to open the connection.
  • Using Console.WriteLine to make sure that we are connected to database.


  • Above we saw that how can we connect to Database using ADO.NET . Now we will try to interact with the database. There are also number of classes and objects and methods in ADO.NET which are used to perform operations on database like Creating Table , Inserting values in the Table , Deleting values, Updating values Etc. Let's write the program to insert values into database Table.
    Below is the program to insert value into table using C Sharp ADO.Net properties.


    using System;

    using System.Data.SqlClient;
    namespace insert_value

    {

          class Program

         {

                static void Main(string[] args)

                {

                        string connection = "data source=VISHAL- PC\\SQL2014;database=Test;uid=sa;password=sa@123";

                        SqlConnection con=new SqlConnection(connection);

                        con.Open();

                        SqlCommand cmd=new SqlCommand("Insert into WinForm_1 (Name,Age)                      values('vkk','20')",con);

                       cmd.ExecuteNonQuery();

                       Console.WriteLine("Record inserted");

                       Console.ReadKey();

               }

          }

    }



    Output.

    Before inserting.


    After inserting.



    Explanation

  • Using System- The System is the namespace containing all of the basic important classes and methods.
  • Using System.Data.Sqlclient- This namespace contains all the required classes to interact with the database.
  • Class Program- is the name of the class.
  • static void main(String [] args)- The main methods where the Console Program starts executing.
  • string connectionstring- is the variable to store the connection to Database.
  • Data Source- is the name of the Database Server.
  • Database- Name of the database to interact with.
  • uid- User id to connect to Database.
  • password- password required for authentication.
  • SqlConnection con=new SqlConnection(connectionstring)-Sqlconnection is the name of the class which is used to establish connection and interact with the Database. We passed the connectionstring to SqlConnection Constructor indication the Database path to connect with.
  • con.open()-Method of sqlconnection class to open the connection.
  • SqlCommand cmd=new SqlCommand(Querey)-Sqlcommand is the Class in ADO.NET with the help of which we can pass the queries to Database. The Default constructor of the SqlCommand Accepts the Query to be passed to SQL Server to perform operation on the Database Table.
  • cmd.ExecuteNonQuery- Method to execute the query passed to Database via SqlCommand Object.
  • C Sharp Program to Connect to Database-Insert Value in Database Table C Sharp Program to Connect to Database-Insert Value in Database Table Reviewed by LanguageExpert on August 24, 2017 Rating: 5

    No comments