Friday 4 August 2017

What is ADO.Net ?

ADO.Net : ADO Stands for Microsoft Activex Data Object is a set of Classes (frameworks) is used as a
intermediatory part between any Dot Net application and Different Datasources like Databases and XML
files.
ADO.Net esrablishes the connection between the datasource (oracle,sql server) and Dot net application. Firstly we have to make a connection with the Database and ASP application by connection string.
After establishes the connetion we need to create the connection object and open the connection.

Once connetion is open then we have to send commands by it , so we need sqlcommand object by which we can send and the queries or stored procedure parameters.

after that we can manuplate the data by datatable or we can read the data by sqlreader.

  <connectionStrings>
    <add name="DBCS" connectionString="Data Souce=PC ;Initial Catalog=Example; Integared Security=true"
          providerName="System.Data.SqlClient" />
  </connectionStrings>

 public static string _connectionStr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        SqlConnection conn = new SqlConnection(_connectionStr);


  using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_Insert_Item_Master";
                    if(conn.State!=ConnectionState.Open)
                    {
                        conn.Open();

                        cmd.Parameters.Add("@ItemTypeID", SqlDbType.Int).Value = objInventoryAll.ItemTypeID;
                        cmd.Parameters.Add("@ItemAddedByUserID", SqlDbType.Int).Value = objInventoryAll.UserID;
          
                        cmd.ExecuteNonQuery();
                    }


On the Above code , SQLconnection class is used for establishes the connection with Database.
SQLCommand class is used for providing the Sql queries to the Sql connection for exexcution.

commandtype shows the type of sql commands.
commandtext shows the name of stored procedure.
con.open() opens the connection.
cmd.parameters.add() it adds the parameters to command object(cmd).


No comments:

Post a Comment

Creating First WEB API Project from scratch ?

  For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure. Now you have to select  ASP.N...