Important ADO.NET Interview Questions
In this article we will discuss about the most asked ADO.NET interview questions and answers. If you need to know other interview questions and answers, I strongly recommend to follow this link: Interview Questions. Now in this post we are going to share the interview questions or the information which you must know as a programmer or a developer, especially if you are a Dot Net developer. I hope you will like this article.
Background
I am a dot net developer. As a dot net developer, you will always use ADO.NET to handle your data. Isn’t it. So it is important that you must know some information. We will read those basic information here. I am sharing those in the form of articles, you can always read my other interview questions here in the below links.
So shall we now discuss about ADO.NET interview questions?
ADO.NET Interview Questions
What is ADO.NET?
ADO.NET is basically a set of components which can be used by a programmer to access and manipulate the data in a disconnected architecture. It gives the access to the data source such as SQL Server and XML. We use DataSet for the data operations in ADO.NET. It is a part of Microsoft .Net framework.
Difference between ExecuteReader, ExecuteScalar and ExecuteNonQuery ?
ExecuteNonQuery
ExecuteNonQuery is basically used for operations where there is nothing returned from the SQL Query or Stored Procedure. Preferred use will be for INSERT, UPDATE and DELETE Operations.
[csharp]
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Persons WHERE Name = @Name", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
con.Close();
}
}
[/csharp]
Execute Scalar
ExecuteScalar is a handy function when you want to just need one Cell value i.e. one column and one row.
For example in a case where I need to get the City of a person based on its Name.
[csharp]
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT City FROM Persons WHERE Name=@Name", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
con.Open();
object o = cmd.ExecuteScalar();
if (o != null)
{
string city = o.ToString();
}
con.Close();
}
}
[/csharp]
Execute Reader
ExecuteReader is strictly used for fetching records from the SQL Query or Stored Procedure i.e. SELECT Operation.
Example would be fetching Name City for all records in the Person Table.
[csharp]
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, City FROM Persons", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string name = dr["Name"].ToString();
string city = dr["City"].ToString();
Response.Write("Name: " + name);
Response.Write("City: " + city);
}
con.Close();
}
}
[/csharp]
What are Data Reader, Dataset & Data Adapter ? Explain the differences?
Data Reader
Data Reader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. Data Reader is used to iterate through result set that came from server and it will read one record at a time because of that memory consumption will be less and it will fetch the data very fast when compared with dataset. Generally we will use Execute Reader object to bind data to data reader.
[csharp]
using (SqlConnection con = new SqlConnection("Connectionstring"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select UserName,LastName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
}
[/csharp]
Dataset
Dataset is a disconnected orient architecture that means there is no need of active connections during work with datasets and it is a collection of Data Tables and relations between tables. It is used to hold multiple tables with data. You can select data form tables, create views based on table and ask child rows over relations. Also Dataset provides you with rich features like saving data as XML and loading XML data.
[csharp]
SqlCommand cmd = new SqlCommand("select UserName,LastName,Location from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
[/csharp]
Data Adapter
DataAdapter will acts as a Bridge between DataSet and database. This dataadapter object is used to read the data from database and bind that data to dataset. Dataadapter is a disconnected oriented architecture.
[csharp]
SqlCommand cmd = new SqlCommand("select UserName,LastName,Location from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
[/csharp]
ADO stands for?
ActiveX Data Object
What are the main objects used in ADO.NET?
Dataset
Dataset is a disconnected orient architecture that means there is no need of active connections during work with datasets and it is a collection of Data Tables and relations between tables. It is used to hold multiple tables with data. You can select data form tables, create views based on table and ask child rows over relations. Also Dataset provides you with rich features like saving data as XML and loading XML data.
DataReader
Data Reader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. Data Reader is used to iterate through result set that came from server and it will read one record at a time because of that memory consumption will be less and it will fetch the data very fast when compared with dataset. Generally we will use Execute Reader object to bind data to data reader.
That’s all. Have a great day.
Conclusion
Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
Your turn. What do you think?
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.
Kindest Regards
Sibeesh Venu