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. So it is important that you must know some information. We will read those basic information here.
So shall we now discuss about ADO.NET interview questions?
ADO.NET Interview Questions
Question: 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 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.
Question: Difference between ExecuteReader, ExecuteScalar and ExecuteNonQuery ?
Click here for source code.
- 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.
- 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();
- }
- }
- 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.
- 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();
- }
- }
- 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 and City for all the records in the Person Table.
- 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();
- }
- }
Question: What is 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.
- using(SqlConnection con = new SqlConnection("Connectionstring"))
- {
- con.Open();
- SqlCommand cmd = new SqlCommand("Select UserName,LastName,Location FROM UserInformation", con);
- SqlDataReader dr = cmd.ExecuteReader();
- }
- DataSet
DataSet is a disconnected oriented 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.
- SqlCommand cmd = new SqlCommand("select UserName,LastName,Location from UserInformation", con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- DataAdapter
DataAdapter 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.
- SqlCommand cmd = new SqlCommand("select UserName,LastName,Location from UserInformation", con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
Question: ADO stands for?
ActiveX Data Object
Question: What are the main objects used in ADO.NET?
Dataset
DataSet is a disconnected oriented 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 from 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
DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetching the data from database. DataReader 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?
If you have any suggestions, then please mention it in the comments section.
Read this article in my blog here.