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

What is anonymous types?

The anonymous types (introduced in 3.0) allows compilers to work with types that were not previously defind. Anonymous types are generally used for on-the-fly types that won't be used elsewhere in the application.
What is the maximum capacity of a DataTable in ADO.NET?

The maximum number of rows a DataTable can hold is 16,777,216.
How to load Dataset?

NOTE: This is objective type question, Please click question title for correct answer.
What are the SqlDataAdapter command properties?

Following are the SqlDataAdapter Command Properties:-

1). InsertCommand
2). DeleteCommand
3). UpdateCommand
4). SelectCommand

InsertCommand is used for writing Insert statement.
DeleteCommand is used for writing Delete statement.
UpdateCommand is used for writing Update statement.
SelectCommand is used for writing Select statement.

For Ex:-
//For select query
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from employee_master");
da.Fill(ds);

we can write queries for Insert,delete and update command also.

where did i post the same content.I have just change from objective to subjective. above is my previous post that is not approving.
Which DataAdapter property is used for writing Select Query?

NOTE: This is objective type question, Please click question title for correct answer.
What datatype does ExecuteNonQuery method return?

NOTE: This is objective type question, Please click question title for correct answer.
Can we use ExecuteNonQuery method for Select statement?

No,we can not use,as ExecuteNonQuery method is only used for DML operations like Insert,Update and Delete operations.
As it only returns an integer value.

We can understand it by an example:-

int row_affected = 0;

SqlConnection con = new SqlConnection("write your connection name");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update employee_master set employee_name = 'Vishal' where employee_id = 1 and status = 'AA'";
cmd.Connection = con;

row_affected = cmd.ExecuteNonQuery();

if (row_affected > 0)
{
//message record successfully updated.
}

What is the best approach to get single column values in ADO.Net?

NOTE: This is objective type question, Please click question title for correct answer.
What are the approaches to get single column values in ADO.Net?

NOTE: This is objective type question, Please click question title for correct answer.
Which SqlCommand Object property is used for passing Query or StoredProcedure name in the code?

NOTE: This is objective type question, Please click question title for correct answer.
Which SqlCommand property we have to set to StoredProcedure if we are using Stored Procedure name in code?

NOTE: This is objective type question, Please click question title for correct answer.
What are the namespaces in which .NET has the data functionality class?

NOTE: This is objective type question, Please click question title for correct answer.
How to convert Datatable into Dataview?

With the help of Datatable DefaultView property we can convert any Datatable into Dataview.

DataView is used for sorting and filtering purposes.

If we want to sort rows based on condition,then we will use DataView Sort method.

For ex:-


DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");

dt.Rows.Add("1", "Vishal");
dt.Rows.Add("2", "Rajesh");
dt.Rows.Add("3", "Prashant");
dt.Rows.Add("4", "Dharmesh");
dt.Rows.Add("5", "Nitin");

DataView dv = dt.DefaultView;
dv.Sort = "ID desc";


We can also Sort using datatable rows with Linq or DataTable'Select method.
But as Dataview is more efficient way to do this.

How to convert DataView back to DataTable?

With the help of DataView' ToTable() method we can convert
DataView back to DataTable.

For ex:-

As we know that,DataView is used for sorting and filtering purposes.
Sometimes,we do not have an Order By clause in the query,so we have such functionality with the help of Dataview Sort property.

For Example:-


DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");

dt.Rows.Add("1", "Vishal");
dt.Rows.Add("2", "Rajesh");
dt.Rows.Add("3", "Prashant");
dt.Rows.Add("4", "Dharmesh");
dt.Rows.Add("5", "Nitin");

DataView dv = dt.DefaultView;
dv.Sort = "Name Desc";

DataTable dt_new = dv.ToTable();

Gridview1.DataSource = dt_new;
Gridview1.DataBind();

Which properties are used to Bind a DataGridView control in Windows application?

The DataSource property and the DataMember property are used to bind a DataGridView control.

Here, DataSource refers the object which binds DataGridView,that's generally Dataset/Datatable/or any object.

DataMember refers to Table Name.
Dataset is of which architecture?

Dataset is a dis-connected architecture,meaning that we do not have to open connection while dealing with databases.

For Example:-

For ex:-


public DataSet Load_Records(int employee_id)
{
string sql_query = "select employee_name,employee_code,address,ph_no from employe_master where employee_id = "+ employee_id +" and status = 'ÁA'";

//write your connection object
Sqlcommand cmd = new Sqlcommand();
cmd.connction = con;//here con is your connection object
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"Employee Master");

return ds;
}



In above function,we can see that,we have not opened connection and closed as well.
Still it will fetch records and fill Dataset.So it's dis-connected architecture.
What does ExecuteReader return?

It returns only SqlDataReader Object.It is associated with Sqlcommand object.

For ex:-

string sql_query = "select employee_name,employee_code from employe_master where employee_id = 1 and status = 'ÁA';


//write your connection object
Sqlcommand cmd = new Sqlcommand();
cmd.connction = con;//here con is your connection object
con.open();

SqlDataReader dr = cmd.ExcuteReader();

If(dr.Read())
{
string employee_code = Convert.ToString(dr["employee_code"]);

string employee_name = Convert.ToString(dr["employee_name"]);

dr.Close();
}
con.Close();

How can we Add or Remove rows from the DataTable object of DataSet?

With the help of Add,Remove and RemoveAt method,we can Add or Remove rows from the DataTable.

Explaination:-

Add() - Adds a new row to DataRowCollection.
Remove()- Removes a DataRow object from DataRowCollection.
RemoveAt() - Removes a row whose location is specified by an index number.
Which object is used to add a relationship between two DataTable objects?

The DataRelation object is used to add relationship between two DataTable objects.

Suppose,we have 2 tables employee_master and project_master in which employee_id is common.

So we can add relation as


DataColumn emp_mas_col = DataSet1.Tables["employee_master"].Columns["employee_id"];
DataColumn prj_mas_col = DataSet1.Tables["project_master"].Columns["employee_id"];

//Now Create Their DataRelation as
DataRelation prj_emp = new DataRelation("prj_emp_master", emp_mas_col,prj_mas_col);

//Now Add the relation to the DataSet.
DataSet1.Relations.Add(prj_emp);

Which property is used to check whether a SqlDataReader is Closed or Opened?

The IsClosed property is used to check whether a SqlDataReader is Closed or Opened.
This property returns a true value if a Data-Reader is Closed,otherwise a false value is returned.
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