• +91 9723535972
  • info@interviewmaterial.com

Ado.net Interview Questions and Answers

Ado.net Interview Questions and Answers

Question - 51 : - How to load multiple tables into a dataset?

Answer - 51 : -

DataSet ds=new DataSet();
SqlConnection con=new SqlConnection("connection_string");
SqlDataAdapter da=new SqlDataAdapter("select * from Employee1",con);
da.Fill(ds.Tables.Add()); 
da=new SqlDataAdapter("select * from Employee2",con);
da.Fill(ds.Tables.Add()); 

After tables have been added into a DataSet, the below-given code tells about how to make use of the DataSet tables. If you decide to use the first table in a dataset or to copy the table data into a data table, then follow the below-given code:

DataTable dt=new DataTable();
dt=ds.Tables[0]; 

The above code can be used to add the required number of tables in a dataset. This ensures connection-less access to data. As the dataset is filled with multiple tables, every time we want to query the data the database connection is not required. It also makes sure about the reusability of data.

Question - 52 : - What is the difference between connected and disconnected architecture in ADO.NET?

Answer - 52 : -

Connected architecture

Disconnected architecture

It is connection-oriented.

It is not connection-oriented.

DataReader is a connected architecture.

DataSet is a disconnected architecture.

High speed and performance are given by connected methods.

Disconnected methods are low in speed and performance.

Data persistence is not possible using DataReader.

Data persistence is possible using DataSet.

It carries the single table data.

It carries data from multiple tables.

We can’t update the data as it is read-only.

Here we can update the data.

Question - 53 : - What is LINQ?

Answer - 53 : -

  • LINQ(Language Integrated Query) is a structured query syntax that helps the programmers and testers to retrieve data from various data sources such as Collections, XML Docs, ADO.NET DataSet, web service, MS SQL Server, etc.
  • It is integrated with C# or VB.NET and it eliminates the mismatch between different programming languages and databases. It provides a single querying interface for various data source types.
  • An object will be returned as a result of LINQ query execution. It will allow you to use an object-oriented approach on the result set and there is no need to worry about the transformation of different result formats into objects.
 

Question - 54 : - How can you identify whether any changes are made to the DataSet object since the time it was last loaded?

Answer - 54 : -

The DataSet object has two methods to track down the changes:

  • GetChanges(): It returns the DataSet object that has been changed since it was loaded or since the execution of the AcceptChanges() method.
  • HasChanges(): It indicates if any modifications were made since from the time the DataSet object was loaded or after a method call to the AcceptChanges() was made.
Use the RejectChanges() method, if you want to reverse the entire changes since from the time the DataSet object was loaded.

Question - 55 : - What is the difference between Dataset.Clone() and DataSet.Copy() methods?

Answer - 55 : -

  • The method Clone() copies only the DataSet structure. The copied structure will have all the constraints, relations, as well as DataTable schemas used by the DataSet. It does not copy the data stored in the DataSet.
  • The Copy() method copies the DataSet structure along with the data in the DataSet. The original data will not be affected.

Question - 56 : - Which methods are provided to add or remove rows from the DataTable object?

Answer - 56 : -

The collection of rows for the DataTable object has been defined by the DataRowCollection class. DataRowCollection class has the method NewRow() for adding a new DataRow to DataTable. This method creates a new row that implements the similar schema that is applied to the DataTable.

The methods provided by the DataRowCollection object are given below:

  • Add()- It adds a newly created row into DataRowCollection.
  • Remove()- It deletes the object DataRow from DataRowCollection.
  • RemoveAt()- It deletes a row for which location is marked by an index number.

Question - 57 : - How to make SQL Server connection in ADO.NET?

Answer - 57 : -

Consider the below example where a connection to the SQL Server has been established. An employee database will be used to connect. The C# code will be:

using (SqlConnection con = new SqlConnection(connectionString))    
{    
  con.Open();         
}
Using block will be useful in closing the connection automatically. It is not required to explicitly call the close() method, because using block will do this implicitly when the code exits the block.

// ConnectionExample.cs

using System;  
using System.Data.SqlClient;  
namespace ConsoleApplicationExample  
{  
    class ConnectionExample  
    {  
        static void Main(string[] args)  
        {  
            new Program().ConnectingMethod();  
        }  
        public void ConnectingMethod()  
        {  
            using (  
                     // Creating Connection  
                     SqlConnection conn = new SqlConnection("data source=.; database=employee; integrated security=SSPI")  
                 )  
            {  
                conn.Open();  
                Console.WriteLine("Connection Has Been Successfully Established.");  
            }  
        }  
    }  
}  
Output:

Connection Has Been Successfully Established.
Press any key to continue...
On execution, if the connection has been established, a message will be displayed on an output window.

If the connection is not created with the help of using a block, a connection must be closed explicitly.

Question - 58 : - What is serialization? Write an example program to serialize a DataSet.

Answer - 58 : -

Serialization is the method of converting an object into a byte stream which can be stored as well as transmitted over the network. The advantage of serialization is that data can be transmitted in a cross-platform environment across the network and also it can be saved in a storage medium like persistent or non-persistent.

The code for serializing a DataSet is:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.IO;
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source=data_source_name;Initial Catalog=employee;Integrated Security=True");  //Create connection object
        SqlDataAdapter da = new SqlDataAdapter("select * from emp", conn);  //DataAdapter creation
        DataSet s = new DataSet();
        da.Fill(s);  
        FileStream fObj = new FileStream("C:\\demo.xml", FileMode.Create);   // Create a XML file
        XmlSerializer sObj = new XmlSerializer(typeof(DataSet));
        sObj.Serialize(fObj, s);  //Serialization of a DataSet
        fObj.Close();
    }
}
In the above given example, the database name is employee and, the table name is emp. The data in a DataSet will be serialized and stored in a demo.xml file by using Serialize() method.

Question - 59 : - What are the data providers in ADO.NET framework?

Answer - 59 : -

Below Data Providers are used in ADO.NET framework.

  • .NET Framework Data Provider for SQL Server – A Data provider that provides access to Microsoft SQL Server 7.0 or later version and it uses the System.Data.SqlClient namespace.
  • .NET Framework Data Provider for OLE DB – A Data Provider that provides access to any database exposed by using OLE DB and it uses the System.Data.OleDb namespace.
  • .NET Framework Data Provider for ODBC – A Data Provider that provides access to any databases exposed by using ODBC and It uses the System.Data.Odbc namespace.
  • .NET Framework Data Provider for Oracle – A Data Provider that provides access to Oracle database 8.1.7 or later versions and it uses the System.Data.OracleClient namespace.

Question - 60 : - What is DataReader Object?

Answer - 60 : -

Datareader is an object of ADO.Net which provides access to data from a specified data source. It consists of classes which sequentially read data from a data source like Oracle, SQL or Access.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners