ADO.NET
As we were previously discussed about “How to access SQL database though LINQ?”. Now we will talk about another way to connect with SQL database using ADO.Net. ADO.net is acronym for Advance Data Object (ADO.net) is a new database technology used by .Net platform. ADO.Net is a set of classes that provides the services like data access and data managing. It has easier coding and is faster and more efficient then LINQ. It supports both connected and disconnected mode of data access.
Firstly ADO (ActiveX Data Object) was introduces for data access from web pages it relies on COM. But ADO wasn’t really efficient to work with databases because it could hold one data source at a time. So, ADO.Net was introduced which relies on managed providers defined by .Net CLR (Common Language Runtime). ADO.Net creates connection with various databases and integrates all of them. ADO.Net works as a bridge between front end and back end database. Main feature of ADO.net that it provides rich set of libraries to create data sharing and data distributed applications. For accessing data it uses some ADO Objects like connection and command objects which include Dataset, DataReader and DataAdepter etc.
Components of ADO.net are:
Dataset: Dataset can work with data in disconnected manner that means it can work with data without knowing source of data coming from.
DataAdapter: It works as a mediator of Dataset and SQL Server for inserting and retrieving data. SqlDataAdapter object combine with Dataset to perform Fill method which perform data access operation.
DataReader: DataReader object works same as Dataset but it only works in connected manner. It can only used for retrieving data not use for updating data.
Simple Example to insert data into SQL.
using System;
using System.Data;
//Namespace used for SQL connection
using System.Data.SqlClient;
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Creation of connection string
string str = "data source=ServerName; initial catalog=Databasename; integrated security=true";
// Create connection with SQL database by passing string parameter
SqlConnection con = new SqlConnection(str);
//Open the connection
con.Open();
//Query to perform insertion
string query = "insert into tableName values @name";
SqlCommand cmd = new SqlCommand(query, con);
// for passing parameter
cmd.Parameters.AddWithValue("@name",txtname.Text);
// for executing Query
cmd.ExecuteNonQuery();
//Closing the connection
con.Close();
}
For more info : cpd-india
Batches: Regular & Weekends for Working Profession
For FREE DEMO CLASS CALL – 011-65164822 / 91- 8860352748
CPD Technologies Block C 9/8, Sector – 7, Rohini, Delhi – 110085, India Landmark: Near Rohini East Metro Station, Opposite Metro Pillar No-397
Website :- www.cpd-india.com
Email :- support@cpd-india.com