A connected famous ADO.NET interview question which is asked is Explain What dataset is a disconnected architecture means?
Step 1 : Import namespace
In ASP to connect to the data the first we need to import the “System.Data.SqlClient” namespace on our code.
Step 2 : Create connection object
The next step is to connect to the database. To connect to the database we need to create the “SqlConnection” object. To create “SqlConnection” we need the connection string. Connection string provides the name of the server , database and security details. Once the connection string is provided we need to open the connection by calling the “Open” method of “SqlConnection” class.
SqlConnection con = new SqlConnection(“Server=ServerNameDatabase=Yourdatabasename Trusted_Connection=True”);
Step 3 :- Create the command object
Once the connection is created we can now fire SQL using that connection. To fire SQL we need to create the “SqlCommand” object and provide the SQL and the connection object created in the first step. In this SQL are searching customer record using “id” field.
SqlCommand cmd = new SqlCommand(“select * from customer where id=1001”,con);
Step 4 :- Load dataset object
Now that we have written the command object it’s time to fire the SQL command. When the SQL command is fired it returns a dataset object which is nothing but data in columns and rows format. To fill dataset we need to use the “SqlDataAdapter” class.
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
Step 5 :- Close the connection.
Once you have got your records and you are done with your operation we need to close the connection to release the memory and resources.