Wednesday, 23 November 2011

Dot Net - C#.Net Interview Questions Some more

Difference between connected and disconnected mode in ado.net:

when "connected" the connection to the server (or database) is maintained in state.
when "disconnected" the connection to the server is not maintained and the changes must be transmitted back to the server to "reconnect" and save the changes to the database
so think of a client/server app like a win form or something
if you are "connected" then the data being moved between the client and the server are being done with an active connection or in "real-time"
if you are "disconnected" then the data is sent to the client where it lives on the client as a datasource. the client can make changes, add new data, delete data, etc...
when the client "reconnects" the data is sent back to the server (either as a diff or as a whole) where the server will save the changes back to the database or datasource
DataReader is connected architecture since it keeps conneection open untill all records are fetched
DataSet is disconnected architecture gets all records at once and closes connection and are available even after connection is closed
Disconnected architecture is a method of retrieving a record set from the database and storing it giving you the ability to do many CRUD (Create, Read, Update, Delete) operations on the data in memory, then it can be re-synchronized with the database when reconnecting. A method of using disconnected artitecture is using a DataSet.
Connected architecture is when you constaintly make trips to the database for any CRUD operation you wish to do. this creates more traffic to the database but is normally much faster as you should be doing smaller transactions.
Another way to think about it is that with a disconnected architecture, once you have the data, you could even unplug your computer from the network. Then do a bunch of queries against the data, sort it, make edits, insert new rows, etc. Then connect to the network again at a later time and submit the updates. A disconnected architecture also lets you easily send the data around to other machines. This approach might be used with a seldomly-connected client, for example. A DataSet could even be serialized to disk to persist it past power-cycling.
With a connected architecture, those types of things aren't possible. You need to have a connection to the DB to do inserts, for example.

2: What are the ways to create connection in ADO.NET?

There are two ways to create connection supported by ADO.NET.
Steps to create connection in code
Create instance of connection object.
Set the ConnectionString property.
Steps to create connection using designer
Drag connection object from the Data tab of the toolbox.
Set the ConnectionString property using properties window.
Access database at runtime using ADO.NET
Steps to connect to database:
Declare and initialize database connection object with appropriate connection stringCreate a DataAdapter and a Dataset object.
Using fill method of the DataAdapter object, load the data into the Dataset Object.
Display the data using data binding feature of controls like Datagrid or Datalist.
Code to connect to database using VB.NET:
Dim m_adptEmp As SqlDataAdapter
Private Sub Page_Load(ByVal sender As System.Object, _
ByValeAsSystem.EventArgs)HandlesMyBase.Load
DimEmpConnAsNewSqlConnection_
("server=(local);database=Employees;Trusted_Connection=yes")
m_adptEmp = New SqlDataAdapter("select * from Emp", EmpConn)
Dim dsEmp As New DataSet
m_adptEmp.Fill(dsEmp, "Emp")

DataGrid1.DataSource = dsEmp.Tables("Emp").DefaultView
DataGrid1.DataBind()
End Sub


1:Differences between “DataSet” and “DataReader”.
.
Dataset:
DataSet object can contain multiple rowsets from the same data source as well as from the relationships between them
Dataset is a disconnected architecture
Dataset can persist data.
A DataSet is well suited for data that needs to be retrieved from multiple tables.
Due to overhead DatsSet is slower than DataReader
Datareader:
DataReader provides forward-only and read-only access to data.
Datareader is connected architecture. It has live connection while reading data
Datareader can not persist data.
Speed performance is better in DataReader.


2: Explain how to load multiple tables in a DataSet:


MyDataSet myds = new MyDataSet();
SqlDataAdapter myda = new SqlDataAdapter ("procId", this.Connection);
myda.SelectCommand.CommandType = CommandType.StoredProcedure;
myda.SelectCommand.Parameters.AddWithValue ("@pId", pId);
myda.TableMappings.Add ("Table", myds.xval.TableName);
myda.Fill (myds);
ADO.NET Code showing Dataset storing multiple tables.:
DataSet ds = new DataSet();
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
ds.Tables.Add(dtn);


3: Explain the basic methods of Dataadapter:
Some basic methods of dataadpater are:
Fill: adds or updates the rows to dataset from the datasource
FillSchema: adds a datatable with the sameschema as in the datasource
Update: it calls insert, update, delete sql commands for transferring all the changes from the dataset to the datasource
Dispose : releases all the resources


4: Difference between DataReader and DataAdapter / DataSet and DataAdapter?

Data Reader will read only one record at a time.So memory consumption will be less,it is purely connection oriented it will use connection exclusively when datareader is closed.the memory will be released on the server machine and connection will be used for any other purpose.

Data Adapter acts as a mediator between dataset and database server.
it will read data from the database server and places with in dataset
the manipulations on the dataset will be updated with the database server
Difference between Dataset and DataReader : Points to be consider while choosing between the DataSet and DataReader objects
DataSet object DataReader object
Read/Write access Read-only access
Supports multiple tables from different databases Supports a single table based on a single SQL query of one database

Disconnected mode Connected mode
Bind to multiple controls Bind to a single control
Forward and backward scanning of data Forward-only scanning of data
Slower access to data Faster access to data
Greater overhead to enable additional features Lightweight object with very little overhead
Supported by Visual Studio .NET tools Must be manually coded
Assembly:
Assembly is a basic unit of .net program and it contains the all .net code, resources, references and versions etc.
.exe and .dll are same while .exe contains executable code and is machine dependent.

6: What is the difference between Namespace and Assembly?

Namespace:

It is a Collection of names wherein each name is Unique.
They form the logical boundary for a Group of classes.
Namespace must be specified in Project-Properties.
Assembly:
It is an Output Unit. It is a unit of Deployment & a unit of versioning. Assemblies contain MSIL code.
Assemblies are Self-Describing. [e.g. metadata,manifest]
An assembly is the primary building block of a .NET Framework application. It is a collection of functionality that is built, versioned, and deployed as a single implementation unit (as one or more files). All managed types and resources are marked either as accessible only within their implementation unit, or by code outside that unit.
4.Assemblies though may at times be equated to dlls its actuall nothing but “Actual Dlls”.
A private assembly is normally used by a single application, and is stored in the application's directory, or a subdirectory beneath. A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful, e.g. the .NET framework classes.
A Windows DLL contains library code to be used by any program running on Windows. A DLL may contain either structured or object oriented libraries
A dynamic-link library (DLL) is an executable file that acts as a shared library of functions
Dot NET assembly is the component standard specified by the .NET. Hence, dot net assemblies are understandable to only Microsoft.NET and can be used only in .NET managed applications.

No comments:

Post a Comment