Ado.net Interview Questions and Answers
Question - 111 : - What are the methods of DataSet?
Answer - 111 : -
It is used in disconnected architecture. It represent records in the form of Database table (Row and Column) format. It stores record of one or more tables.
SqlDataAdapter da;
DataSet ds;
string strconn = "Data Source=YourServerName;Initial Catalog=EMP;Integrated Security=True";
private void Form1_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from userdet", strconn);
ds = new System.Data.DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
C#
Methods of DataSet:
AcceptChanges(): This method saves changes which are made with records in a DataSet.
- Clear(): This method clears (removes) all rows from DataSet.
- Clone(): The clone method copy the structure of DataSet. Means it copy only schema not full records of DataSet.
- Copy(): It copies the whole records with structure of DataSet.
- RejectChanges(): This method discard changes which is made with DataSet and set the DataSet to previous stage (which was at first).
- HasChanges(): This method return boolean value to show whether record of DataSet has changed or not. It returns true if any changes has made and false if no other changes made.
- GetChanges(): This method keep copy of those record, which is changed or modified.
Question - 112 : - What you understand by ExecuteNonQuery Method?
Answer - 112 : -
The ExecuteNonQuery method is used to execute the command and return the number of rows affected.
The ExecuteNonQuery method cannot be used to return the result set.
Snippets working with ExecuteNonQuery
public void CallExecuteNonQuery()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM EMP WHERE DEPTNO = 40";
cmd.CommandType = CommandType.Text;
conn.Open();
Int32 RowsAffected = cmd.ExecuteNonQuery();
MessageBox.Show(RowsAffected + " rows affected", "Message");
cmd.Dispose();
conn.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Question - 113 : - What do you understand by DataRelation class?
Answer - 113 : -
The DataRelation is a class of disconnected architecture in the .NET framework. It is found in the System.Data namespace. It represents a relationship between database tables and correlates tables on the basis of matching column.
- DataRelation drel;
- drel = new DataRelation("All", ds.Tables[0].Columns[0], ds.Tables[1].Columns[0]);
Question - 114 : - How can create a SqlConnection?
Answer - 114 : -
SqlConnection class is used to establish the connection between front end and back end.
Syntax:
SqlConnection obj=new SqlConnection(“Integrated Security=true;Initial Catalog=Table_Name;Data Source=.”);-- for Windows authentication
SqlConnection obj=new SqlConnection(“user id= sa ; Password=sa123;server=.;database=name”); --Sql server Authentication
Question - 115 : - What do you understand by SqlTransaction class in ADO.NET?
Answer - 115 : -
The SqlTransaction class is an important class of .NET Framework. It ensures that a body of code will affect a Database or kept the same as previous (Rollback).
At first we should know about it's two most important method which will be used here. They are given below.
- Commit(): It commits the transaction. It save changes made in Database during transaction. In simple term we can also say that it shows the end of transaction at that time.
- Rollback(): It is used to rollback the transaction. It set the database in previous stage which was, before the begin of transaction.
Question - 116 : - Explain the ExecuteReader method
Answer - 116 : -
- The DataReader object is a forward-only and read-only cursor.
- It requires a live connection to the data source.
- The DataReader object cannot be directly instantiated. Instead, we must call the ExecuteReader() method of the command object to obtain a valid DataReader object.
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Question - 117 : - Explain the ExecuteScalar method in ADO.NET?
Answer - 117 : -
The ExecuteScalar Method in SqlCommandObject returns the first column of the first row after executing the query against the Data Source.
- If the result set contain more than one column or rows, it takes only the first column of the first row. All other values are ignored.
- If the result set is empty it will return null.
Int32 TotalSalary = Convert.ToInt32(cmd.ExecuteScalar());
Question - 118 : - Explain the ExecuteXmlReader?
Answer - 118 : -
The execute reader method is flexible when we need the result set in the form of an XML doucment. The ExecuteXmlReader methods returns an instance of XmlReader class.
Example:
XmlReader xmlreader = cmd.ExecuteXmlReader();
XmlDocument xdoc = new XmlDocument();
C#
Using the XmlDocument class we load the XmlReader object and save it to the File System using the Save method.