ADO.Net Interview Questions and Answers for Freshers, Experienced

    61 Votes

9) Write a sample ADO.Net Connection to SQL Server ?

using System.Data.SqlClient;

try
{
// Open DB connection
string Connection = "server=localhost; uid = UserName ; pwd = Password ; database = DemoDB";
SqlConnection conn = new SqlConnection(Connection);
conn.Open();
}
catch ( Exception ex )
{
// Log Exceptions
}

finally
{
conn.Close ( ) ; // Close the connection
}

10) Explain Transactions in ADO.Net ?

When one or more related SQL Commands are executed and if any one statement failed to execute, we need to rollback the entire related operations that has been executed. In such a scenario, Transactions are used.

using System.Data.SqlClient;
try
{
string Connection = "server=localhost; uid = UserName ; pwd = Password ; database = DemoDB";
SqlConnection con = new SqlConnection(Connection);
con.Open();

SqlTransaction SqlTx = con.BeginTransaction();
// Execute SQL commands
tx.Commit(); // Commit Transaction
con.Close();


Also Read - 
.Net Framework Interview Questions and Answers

11) Explain ReadCommitted and ReadUncommitted in Transactions ?

All simple Transactions are ReadCommitted by default. If two persons are accessing the same table and if one person executes an insert statement, another person will be able to access the newly inserted row only after the first person commit the transaction. If a transaction is set as ReadUncommited, in the two person scenario second person will be able to read the information inserted by first person before he commits the transaction.


12) Explain SqlCommand Objects ?

SQLCommand in simple terms is a collection of strings containing SQL statements which will be send to database for various operations. SQL Commands can be of type Select, Insert, Delete,Update or Stored procedures.

string Connection = "server=localhost; uid = UserName ; pwd = Password ; database = DemoDB";
SqlConnection con = new SqlConnection(Connection);
con.Open();

string select = "Select * from Employee_Table";
SqlCommand cmd = new SqlCommand(select, conn);
con.Close();


Also Read - 
C# OOPS Interview Questions and Answers

13)
What are the different Command types in Commands ?

Different Command Types are Default, CommandType.StoredProcedure and CommandType.TableDirect .

For normal queries like select , command type is not specified .

string select = "Select * from Employee_Table";
SqlCommand cmd = new SqlCommand(select, conn);


For Stored Procedures, command type should be specified as shown below

SqlCommand cmd = new SqlCommand("Employee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmployeeID", "EMP-761");

For TableDirect (fetch all rows and columns from a table) , command type should be specified as shown below

OleDbCommand cmd = new OleDbCommand("Employees", conn);
cmd.CommandType = CommandType.TableDirect;


14) What are the different execution methods provided by the command object ?

After creating an SQL or OLEDb Command, it needs to be executed. Following methods are used to execute SQL Commands.

  • ExecuteNonQuery() – Used to execute the command which did not return any output
  • ExecuteReader() – Used to execute the command which return a typed IDataReader
  • ExecuteScalar() – Used to execute the commands which return a single value
  • ExecuteXmlReader() – Used to execute the command that returns an XmlReader object. The object can be used to traverse the XML fragment returned from DB.


15) What is a Dataset object ?

The DataSet is used to store data from a data source . It consists of a set of data tables. We can also specify the relation between the tables inside a dataset.


16) What is a Datatable object ?

Data Table is used to store the data retrieved from a database in application memory. Data table will have a set of data columns and data rows. Data table supports operations like adding, updating and deleting a row.

Page 2 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C# Interview Questions
.Net Framework Interview Questions and Answers
ASP.Net Interview Questions and Answers
ASP.Net MVC Interview questions and Answers
WCF Interview Questions and Answers
ASP.Net Web API Interview Questions
Top 50 Entity framework interview questions & answers