ADO.NET Interview Questions and Answers (445) - Page 21

What are the core objects of ADO.Net?

NOTE: This is objective type question, Please click question title for correct answer.
Which ADO.Net object is used for fetching records in Forward-Only manner?

SqlDataReader is the only ADO.Net object which is used for fetching records in Forward-Only manner.

As we know that,Datareader fetches records one-by-one from DB.So there is no Back-forth.
Which property of SqlDataReader object is used for getting no of columns in row?

SqlDataReader'FieldCount property is used for getting no of columns in current row.
It returns no of columns as integer value means it counts columns.

int no_of_columns = dr.FieldCount;

where dr is our SqlDataReader object.
How to get No of columns in DataSet/DataTable object?

With the help of Dataset/Datatable Columns Count propertymwe can check that how many columns are dataset having.

For Example:-

int Count = Dt.Columns.Count.

Here,Dt is our dataTable Object.
How to check whether SqlDataReader contains any rows or not?

SqlDataReader has HASROW property,which returns true,if it contains any row otherwise returns false.

For Example:-
if(dr.HasRows)

{
}

What does DataReader'Read() method do?

Datareader Read() method reads records one-by-one in a forward manner only.
As name suggests Read means reading records from database.
If records found in DB,then Read method returns true otherwise returns false;

if(dr.Read())

{
if(!isdbnull(dr["employee_name"]) employee_name = Convert.toString(dr["employee_name"]);
}


Here dr is our SqldataReader Object.
How to retrieve only the structure of a DataTable i.e. all the columns?

DataTable Clone() method is used for retrieving all the columns a DataTable have.

It only retrieve structure not rows.

For Example:-

DataTable new_dt = new DataTable();

new_dt = dt_employee.Clone();


So,whatever dt_employee Datatable contains columns will be assigned to new_dt DataTable.

Here dt_employee is another DataTable.
How to Remove Rows from DataTable at specified index position?

Use RemoveAt() method of DataTable'Rows property to remove records at specified position.

For Example:-
dt_employee.Rows.RemoveAt(0);

Here dt_emplyee is our DataTable object and 0 is our DataTable Row Index or Position.
So it will remove 1st records from Datatable as Datatable rows start from 0 index.
How to copy structure as well as data of a DataTable i.e. all the columns with rows?

We can use DataTable'Copy() method to have all the columns as well as rows into another dataTable.

For Example:-

DataTable dt_new = new DataTable();
dt_new = dt_employee.Copy();

Here,dt_employee is our another DataTable which contains Columns as well as Rows.

So here,dt_new DataTable will be assigned with all the Columns and Rows.
How to reset Datatable/Dataset to its Original state meaning no tables,no colums and no rows?

We can again initialize datatable,then it will reset.
For Example:-Suppose i have dt_employee DataTable,so to reset it

i will create new object as

dt_employee = new DataTable();


Otherway we can do is to use DataTable Reset() method ti achieve the same.

For Example:-
dt_employee.Reset();

How many ways to clear Columns as well as Rows in DataTable?

We can clear both DataTable Rows and Columns by creating new Objects as well as with the help of Clear() method of Datatable Column/Rows property.

dt_employee.Columns.Clear(); //For clearing Columns

dt_employee.Rows.Clear(); //For clearing Rows
dt_employee = new DataTable(); //For clearing both Columns and Rows

What is the use of Merge method in Ado.Net?

Merge is used for merging one Datatable/Dataset to another Datatable/Dataset.it's just like Copy() method in Datatable/Dataset.

For Example:-

DataTable dt_employee = new DataTable();
DataTable dt_merge = new DataTable();

dt_merge.Merge(dt_employee);

Here,All the columns as well as rows of dt_employee will be copied or merged to dt_merge DataTable.
Merge method will have no return type.
How many ways we can merge one DataTable into another DataTable?

NOTE: This is objective type question, Please click question title for correct answer.
How to add columns in DataTable?

DataTable has Columns property and Columns has Add static method,so with the help of Add method we can add any columns into DataTable.

For Example:-

DataTable dt_employee = new DataTable();

dt_employee.Columns.Add("Employee_Code");
dt_employee.Columns.Add("Employee_Name");


Here,Employee_Code and Employee_Name will be treated as DataTable Columns.
How to Remove particular column from DataTable at specified Index position?

With the help of RemoveAt() method of DataTable' Column property,we can remove any column from specified index position.

For Example:-

DataTable dt_employee = new DataTable();

dt_employee.Columns.Add("Employee_Code");
dt_employee.Columns.Add("Address");
dt_employee.Columns.RemoveAt(0); //where 0 is the first position,so it will remove Employee_Code


Now,the dt_employee dataTable have only one coulmn as Address.
How to check whether Connection is Open or not?

With the help of State property of SqlConnection object,we can check whether Connection is Open or Close.
It has ConnectionState as Enum which has Open value.

For Example:-
if (con.State == ConnectionState.Open)

con.Close();


Here con is our SqlConnection object.
How to Add an Empty row into DataTable?

We can Add an Empty row into DataTable with the help of Rows static method.

DataTable dt_employee = new DataTable();

dt_employee.Columns.Add("Name");
dt_employee.Columns.Add("Address");

dt_employee.Rows.Add(string.Empty, string.Empty);

How do we call and execute a Stored Procedure in Dot Net?

By Using SqlCommand object of System.Data.SqlClient namespace,we can execute our Stored Procedure from Code-Behind

We have to pass Stored Procedure Name in CommandText property of SqlCommand object as:-

cmd.CommandType = CommandType.StoredProcedure;

cmd.Commandtext = "our stored procedure name";
cmd.ExecuteNonQuery();


Here,cmd is or SqlCommand object.
DataSet and DataTable class are present in which NameSpace?

System.Data is the namespace used for working with DataSet and DataTable classes in Dot Net.

We have to import System.Data at the top of every Page or Form.

using System.Data; //in C#

imports System.Data //in VB.Net

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories