ado.net difference faqs compiled- 1

8
ADO.NET Difference FAQs-1 1. What are the differences between DataReader and DataAdapter? S.No DataReader DataAdapter 1 Works in Connected Mode Works in Disconnected Mode 2 Can have only one record at a time Can have more than 1 records 3 Is ForwardOnly and Readonly Can navigate front and back and editable 4 Faster Slower 2. What are the differences between DataSet and DataReader? S.No DataSet DataReader 1 The data store whose records have to be manipulated can be disconnected. You can read data from datareader only if the connection to data store exists. 2 You have the provision to cache data fetched from data store when using dataset. Caching of data is not possible. 3 Dataset objects have XML Support. XML Support is not provided by datareader. 4 A single dataset object can contain collection of datatable objects wherein each datatable object refers to a table in the datastore. Hence you can manipulate on multiple tables using a dataset. Datareader can read records fetched by a command object containing a single query. If you have to fetch data from multiple tables, then datareader is not advantageous when compared to dataset. 5 Using dataset, you can read the records fetched in any order. While reading records, you can iterate only in forward direction. 6 Performance wise, dataset is slow because it has multiple tables which in turn include multiple rows, columns and constraints. Datareader gives high performance and records can be fetched faster when compared to dataset. 3. What is the difference between DataSet.Copy() and DataSet.Clone()? S.No DataSet.Copy() DataSet.Clone() 1 DataSet.Copy() copies both the structure and data DataSet.Clone() copies the structure of the DataSet, including all DataTable schemas, relations,

Upload: umar-ali

Post on 10-May-2015

1.083 views

Category:

Technology


2 download

DESCRIPTION

This is compiled version(part-1) of ADO.NET Difference FAQS

TRANSCRIPT

Page 1: ADO.NET difference  faqs compiled- 1

ADO.NET Difference FAQs-1

1. What are the differences between DataReader and DataAdapter?

S.No DataReader DataAdapter

1 Works in Connected Mode Works in Disconnected Mode

2 Can have only one record at a time

Can have more than 1 records

3 Is ForwardOnly and Readonly Can navigate front and back and editable

4 Faster Slower

2. What are the differences between DataSet and DataReader?

S.No DataSet DataReader

1 The data store whose records have to be manipulated can be disconnected.

You can read data from datareader only if the connection to data store exists.

2 You have the provision to cache data fetched from data store when using dataset.

Caching of data is not possible.

3 Dataset objects have XML Support.

XML Support is not provided by datareader.

4 A single dataset object can contain collection of datatable objects wherein each datatable object refers to a table in the datastore. Hence you can manipulate on multiple tables using a dataset.

Datareader can read records fetched by a command object containing a single query. If you have to fetch data from multiple tables, then datareader is not advantageous when compared to dataset.

5 Using dataset, you can read the records fetched in any order.

While reading records, you can iterate only in forward direction.

6 Performance wise, dataset is slow because it has multiple tables which in turn include multiple rows, columns and constraints.

Datareader gives high performance and records can be fetched faster when compared to dataset.

3. What is the difference between DataSet.Copy() and DataSet.Clone()?

S.No DataSet.Copy() DataSet.Clone()

1 DataSet.Copy() copies both the structure and data

DataSet.Clone() copies the structure of the DataSet, including all DataTable schemas, relations,

Page 2: ADO.NET difference  faqs compiled- 1

and constraints and it does not copy any data

4. What are the differences between RecordSet and DataSet?

S.No RecordSet DataSet

1 RecordSet provides data of one row at an instant

DataSet is a data structure which represents the complete table data at the same time

2 RecordSet always needs an Open connection to read the data from data sources

DataSet needs connection only for retrieving the data. After retrieve the data connection is not necessary

3 RecordSet is able to load the structure and data of only one table at a time

DataSet has the capability to store the structure and data of multiple tables at a time

4 RecordSet does not support constraints of Databases

DataSet enforces data integrity by using Constraints

5. What are the differences between ADO and ADO.Net?

S.No ADO ADO.Net

1 It is a COM based library. It is a CLR based library.

2 Classic ADO requires active connection with the data store.

ADO.NET architecture works while the data store is disconnected.

3 Locking feature is available. Locking feature is not available.

4 Data is stored in binary format. Data is stored in XML.

5 XML integration is not possible. XML integration is possible.

6 It uses the object named Recordset to reference data from the data store.

It uses Dataset Object for data access and representation.

7 Using Classic ADO, you can obtain information from one table or set of tables through join. You cannot fetch records from multiple tables independently.

Dataset object of ADO.NET includes collection of DataTables wherein each DataTable will contain records fetched from a particular table. Hence multiple table records are maintained independently.

8 Firewall might prevent execution of Classic ADO.

ADO.NET has firewall proof and its execution will never be interrupted.

9 Classic ADO architecture includes client side cursor and server side cursor.

ADO.NET architecture doesn't include such cursors.

Page 3: ADO.NET difference  faqs compiled- 1

10 You cannot send multiple transactions using a single connection instance.

You can send multiple transactions using a single connection instance.

Reference: http://onlydifferencefaqs.blogspot.in/2012/07/adonet-difference-faqs-1.html

ADO.Net Difference FAQs-2

1.Difference between Typed DataSet and Untyped DataSet

S.No Typed DataSet Untyped DataSet

1 It provides additional methods, properties and events and thus it makes it easier to use.

It is not as easy to use as strongly typed dataset.

2 They have .xsd file (Xml Schema definition) file associated with them and do error checking regarding their schema at design time using the .xsd definitions.

They do not do error checking at the design time as they are filled at run time when the code executes.

3 We will get advantage of intelliSense in VS. NET.

We cannot get an advantage of intelliSense.

4 Performance is slower in case of strongly typed dataset.

Performance is faster in case of Untyped dataset.

5 In complex environment, strongly typed dataset's are difficult to administer.

Untyped datasets are easy to administer.

Typed DataSets use explicit names and DataTypes for their members.ex:northwindDataSet.Products.ProductNameColumn.Caption = "pnames";

UnTyped DataSets use table and column collections for their membersex:ds.Tables["emp"].Columns["eno"].ReadOnly=true;

2.Difference between DataView and DataTable

S.No DataView DataTable

1 Read-only i.e., DataView can be used to select the data.

Read/Write i.e., Datatable can be used to edit or select or delete or insert a data.

Page 4: ADO.NET difference  faqs compiled- 1

2 Is a reference to an existing DataTable. Cannot be populated from scratch; must be instantiated with a reference to an existing DataTable.

Can be created empty and then populated

3 Data is a reference to an existing DataTable, and does not consume space.

Data takes storage space.

4 Can sort or filter rows without modifying the underlying data. Rows and columns can be hidden and revealed repeatedly.

Can add/edit/delete rows, columns, and data, and all changes are persistent.

5 Can return a DataTable version of the view

Can be cloned

6 A live reference to a DataTable; any changes in the DataTable data is immediately reflected in the view.

Is source data; does not contain references

7 Supports calculated columns, which are columns with a value calculated on the fly by combining or manipulating other columns.

Does not support calculated columns

8 Can hide or show selected columns

No row or column hiding

3.Difference between Connected and Disconnected Environment

S.No Connected Environment Disconnected Environment

1 Connected Environment needs a constantly connection of user to data source while performing any operation.

Disconnected Environment does not need any connection.

2 Only one operation can be performed at a time in connection Environment.

Multiple operations can be performed.

3 DataReader is used in Connection Environment.

DataSet is used in it.

4 It is slower in speed. Disconnected Environment has a good speed.

Page 5: ADO.NET difference  faqs compiled- 1

5 We get updated data in it. There is a problem of dirty read.

Reference: http://onlydifferencefaqs.blogspot.in/2012/08/adonet-difference-faqs-2.html

ADO.NET Difference FAQs-3

1.Difference between ExecuteNonQuery() and ExecuteScalar() methods in ADO.NET

S.No ExecuteNonQuery() ExecuteScalar()

1 It will work with Action Queries only (Create,Alter,Drop,Insert,Update,Delete).

It will work with Non-Action Queries that contain aggregate functions.

2 It returns the count of rows effected by the Query.

It returns the first row and first column value of the query result.

3 Return type is int Return type is object.

4 Return value is optional and can be assigned to an integer variable.

Return value is compulsory and should be assigned to a variable of required type.

Example-1 for ExecuteNonQuery Method -Insert:

SqlCommand cmd = new SqlCommand("Insert Into SampleTable Values('1','2')",con);//con is the connection object

con.Open();cmd.ExecuteNonQuery(); //The SQL Insert Statement gets executed

Example-2 for ExecuteNonQuery Method - Update:

public void UpdateEmployeeEmail(){SqlConnection conn = new SqlConnection(connString))String sqlQuery = "UPDATE Employee SET empemail='[email protected]' WHERE empid=5;SqlCommand cmd = new SqlCommand(sqlQuery, conn);try{conn.Open();cmd.ExecuteNonQuery();}catch (Exception ex){Console.WriteLine(ex.Message);}finally{

Page 6: ADO.NET difference  faqs compiled- 1

conn.Close();}return count;}

Example-1 for ExecuteScalar Method:

This returns only one value that is first column value of the first row in the executed query

public int getSomeProdId(){int count=0;SqlConnection conn = new SqlConnection(connString))String sqlQuery = "SELECT COUNT(*) FROM dbo.region";SqlCommand cmd = new SqlCommand(sqlQuery, conn);try{conn.Open();//Since return type is System.Object, a typecast is mustcount = (Int32)cmd.ExecuteScalar();}catch (Exception ex){Console.WriteLine(ex.Message);}finally{conn.Close();}return count;}

Example-2 for ExecuteScalar Method:

This returns one value only, no recordsets.

cmd.CommandText = "Select Name, DOB, from Emp where ID=1";Dim strName As string = cmd.ExecuteScalar.ToString

2.Difference between ExecuteNonQuery() and ExecuteReader() methods in ADO.NET

S.No ExecuteNonQuery() ExecuteReader()

1 It will work with Action Queries only (Create,Alter,Drop,Insert,Update,Delete).

It will work with Action and Non-Action Queries (Select)

Page 7: ADO.NET difference  faqs compiled- 1

2 It returns the count of rows effected by the Query.

It returns the collection of rows selected by the Query.

3 Return type is int Return type is DataReader.

4 Return value is optional and can be assigned to an integer variable.

Return value is compulsory and should be assigned to an another object DataReader.

Example-1 for ExecuteNonQuery Method -Insert:

SqlCommand cmd = new SqlCommand("Insert Into SampleTable Values('1','2')",con);//con is the connection object

con.Open();cmd.ExecuteNonQuery(); //The SQL Insert Statement gets executed

Example-2 for ExecuteNonQuery Method - Update:

public void UpdateEmployeeEmail(){SqlConnection conn = new SqlConnection(connString))String sqlQuery = "UPDATE Employee SET empemail='[email protected]' WHERE empid=5;SqlCommand cmd = new SqlCommand(sqlQuery, conn);try{conn.Open();cmd.ExecuteNonQuery();}catch (Exception ex){Console.WriteLine(ex.Message);}finally{conn.Close();}return count;}

Example for ExecuteReader Method:

Here, ExecuteReader is used to get set of records by specified query, namely, "select * from emp"

SqlConnection con = new SqlConnection(constr); //constructor can be connection of string.SqlCommand cmd = new SqlCommand ("select * from emp", con);con.Open();SqlDataReader dr = cmd. ExecuteReader (CommandBehavior. CloseConnection); //Implicitly closes the connection because CommandBehavior. CloseConnection was specified.

Page 8: ADO.NET difference  faqs compiled- 1

while(dr.Read()){Console.WriteLine (dr.GetString(0));}dr.Close();

Reference: http://onlydifferencefaqs.blogspot.in/2012/08/adonet-difference-faqs-3.html