Professional Documents
Culture Documents
net
what is the namespace in which .net has the data functionality classes?
Ans; system.data.
Ans: ADO.NET provides consistent access to data sources such as Microsoft SQL Server, as well as
data sources exposed through OLE DB and XML. Data-sharing consumer applications can use
ADO.NET to connect to these data sources and retrieve, manipulate, and update data.
The ADO.NET classes are found in System.Data.dll, and are integrated with the XML classes found in
System.Xml.dll.
Ans:- The DataTable Select Method - This method is overloaded to accept arguments to filter and
sort data rows returning an array of DataRow objects.
The DataView object sort, filter and find methods - This object uses the same filter arguments
supported by the Select method, but the DataView extrudes structures that can be bound to data-
aware controls.
DataReader is a good choice when retrieving large amounts of data because the data is not
cached in memory
From an ADO.NET implementation perspective, the Recordset object in ADO is eliminated in the
.NET architecture. In its place, ADO.NET has several dedicated objects led by the DataSet object
and including the DataAdapter, and DataReader objects to perform specific tasks. In addition,
ADO.NET DataSets operate in disconnected state whereas the ADO RecordSet objects operated
in a fully connected state.
In ADO, the in-memory representation of data is the recordset. In ADO.NET, it is the dataset.
If you want to access a database multiple times, you should establish a connection using the
Connection object. You can also make a connection to a database by passing a connection string
via a Command or Recordset object. However, this type of connection is only good for one
specific, single query.
What is the use of command objects and what are the methods provided by the command
object ?
ANs :- The ADO Command object is used to execute a single query against a database. The
query can perform actions like creating, adding, retrieving, deleting or updating records.
If the query is used to retrieve data, the data will be returned as a RecordSet object. This means
that the retrieved data can be manipulated by properties, collections, methods, and events of the
Recordset object.
The major feature of the Command object is the ability to use stored queries and procedures with
parameters.
Methods,
i) Fill - adds or refreshes rows in DataSet to match those in datasource using the DataSet name,
and create DataTable named "Table".
ii) FillSchema - adds a DataTable named "Table" to the specified DataSet and configures the
schema to match that in the data source based on the specified SchemaType.
iii) GetFillParameters - retrieves the parameters set by the user when executing a SQL select
statement.
iv) Update - Calls the respective insert, update or delete statements for respective action in the
specified DataSet from a DataTable named "Table".
How do we use stored procedure in ADO.NET and how do we provide parameters to the stored
procedures?
Ans :- Create a command object (SqlCommand, etc) and specify the stored procedure name
Set the CommandType property of the command object to the CommandType.StoredProcedure
enumeration value. This tells the runtime that the command used here is a stored procedure.
What are steps involved to fill a dataset ?(Twist :- How can we use dataadapter to fill a dataset ?)
Ans:- Defining the connection string for the database server
Defining the connection (SqlConnection, OleDbConnection, etc) to the database using the
connection string
Defining the command (SqlCommand, OleDbCommand, etc) or command string that contains the
query
Defining the data adapter (SqlDataAdapter, OleDbDataAdapter, etc) using the command string
and the connection object
Creating a new DataSet object
If the command is SELECT, filling the dataset object with the result of the query through the data
adapter
Reading the records from the DataTables in the datasets using the DataRow and DataColumn
objects
If the command is UPDATE, INSERT or DELETE, then updating the dataset through the data
adapter
Accepting to save the changes in the dataset to the database
SqlDataAdapter objDataAdapter = new SqlDataAdapter ("Select
CompanyName, ContactName, City, Country, Region from
Suppliers", objConnect);
objDataAdapter.Fill (objDS);
What are the various methods provided by the dataset object to generate XML?
Ans:- The DataSet is capable of reading and writing its data and schema as XML. This is
important if you consider that XML is an open, industry standard that is popular among most
software solutions providers. At this time, XML is pervasive - found both in Internet solutions and
in traditional applications. The fact that it is designed to both contain and describe data (data that
describes itself) makes it the perfect choice for a universal data container such as the DataSet.
Furthermore, the ability of a DataSet to both read and write data and schema as XML makes it
possible for you to both create and modify data in a DataSet using XML or XML enabled solution,
such as SQL Server 2000 and VFP. The methods for reading XML into a DataSet have, of course,
complimentary means of writing XML from a DataSet: WriteXml and WriteXmlSchema. Two
additional methods provided are: GetXml and GetXmlSchema. These methods return the data or
schema as a string.
da.Update(ds, "Article");
Here ‘da’ and ‘ds’ are the references of the DataAdapter and DataSet objects respectively.
How can we check that some changes have been made to dataset since it was loaded?(Twist:-
how can we cancel all changes done in dataset? How do we get changed value dataset?)
For tracking down changes Dataset has two methods which comes as rescue
“GetChanges“ and “HasChanges”.
1.GetChanges
Return’s dataset which are changed since it was loaded or since Acceptchanges was
executed.
2.HasChanges
This property indicate’s has any change’s been made since the dataset was loaded or
acceptchanges method was executed.
If we want to revert or abandon all change’s since the dataset was loaded use
“RejectChanges”.
Dataset
1)It is disconnected with database
2)It is both readonly and forward only
DataReader
1)It is connected with database
2)It is forward only
In a DataSet with multiple DataTable objects, you can use DataRelation objects to relate one table
to another, to navigate through the tables, and to return child or parent rows from a related table.
The arguments required to create a DataRelation are a name for the DataRelation being created,
and an array of one or more DataColumn references to the columns that serve as the parent and
child columns in the relationship. After you have created a DataRelation, you can use it to navigate
between tables and to retrieve values.
Adding a DataRelation to a DataSet adds, by default, a UniqueConstraint to the parent table and
a ForeignKeyConstraint to the child table. For more information about these default constraints, see
Adding Constraints to a Table.
The following code example creates a DataRelation using two DataTable objects in a DataSet.
Each DataTable contains a column named CustID, which serves as a link between the two
DataTable objects. The example adds a single DataRelation to the Relations collection of the
DataSet. The first argument in the example specifies the name of the DataRelation being created.
The second argument sets the parent DataColumn and the third argument sets the child
DataColumn.
What the CommandBuilder can do is relieve you of the responsibility of writing your own action
queries by automatically constructing the SQL code, ADO.NET Command objects, and their
associated Parameters collections given a SelectCommand. Cool. And it works, but only in a very
limited way, and for a price, as I'll describe in this article. And unlike the DACW, the code and
commands created by the CommandBuilder are unseen—in another black box.
In pessimistic locking when user wants to update data it locks the record and till then no
one can update data. Other user’s can only view the data when there is pessimistic
locking. In optimistic locking multiple users can open the same record for updating, thus
increase maximum concurrency. Record is only locked when updating the record. This is
the most preferred way of locking practically. Now a days browser based application is
very common and having pessimistic locking is not a practical solution.
Define a Datetime stamp field in the table.When actually you are firing the
UPDATE SQL statements compare the current timestamp with one existing in
the database. Below is a sample SQL which checks for timestamp before
updating and any mismatch in timestamp it will not update the records. This is
the best practice used by industries for locking.
Update table1 set field1=@test where
LastTimeStamp=@CurrentTimeStamp
Check for original values stored in SQL SERVER and actual changed values.
In stored procedure check before updating that the old data is same as the
current. Example in the below shown SQL before updating field1 we check
that is the old field1 value same. If not then some one else has updated and
necessary action has to be taken.
Update table1 set field1=@test where field1 = @oldfield1value
The most common sequence of steps that would be performed while developing a
transactional application is as follows:
Open a database connection using the Open method of the connection object.
Execute the SQL commands using the command object. We may use one or
more command objects for this purpose, as long as the Transaction property of
all the objects is set to a valid transaction object.
Commit or roll back the transaction using the Commit or Rollback method of
the transaction object.
What is CLR?
Common Language Runtime (CLR) manages the execution of code and provides
different services like Garbage collection and support for Base Class Libraries etc.
The main constituents of CLR are described below
The common Language Runtime (CLR) a rich set of features for cross-language
development and deployment. CLR supports both Object Oriented Languages as
well as procedural languages. CLR provides security, garbage collection, cross
language exception handling, cross language inheritance and so on.
All compilers under .NET will generate Intermediate Language no matter what
language is used to develop an application. In fact, CLR will not be aware of the
language used to develop an application. All language compilers will generate a
uniform, common language called Intermediate Language. For this reason IL can
be called as The language of CLR A platform for cross language
development.
What is CLS?
It provides the set of specificaiton which has to be adhered by any new language
writer / Compiler writer for .NET Framework. This ensures Interoperability. For
example: Within a ASP.NET application written in C#.NET language, we can refer to
any DLL written in any other language supported by .NET Framework. As of now
.NET Supports around 32 languages.
what is Assembly?
Assemblies are the building blocks of .NET Framework applications; they form the fundamental
unit of deployment, version control, reuse, activation scoping, and security permissions. An
assembly is a collection of types and resources that are built to work together and form a logical
unit of functionality. An assembly provides the common language runtime with the information
it needs to be aware of type implementations. To the runtime, a type does not exist outside the
context of an assembly.
Public assembly- are the dll/exe file that can be used in different application. The main advantage
of public assemblies is code reusability.
Shared assembly- to run multiple versions of an application or component on the same computer.
What is Namespace?
Namespace is a group of classes, structures, interfaces, enumerations, and delegates,
organized in a logical hierarchy by function, that enable you to access the core
functionality you need in your applications.
Namespace:
1) it is a Collection of names wherein each name is Unique.
2)They form the logical boundary for a Group of classes.
3)Namespace must be specified in Project-Properties.
Assembly:
1) It is an Output Unit.
2)It is a unit of Deployment & a unit of versioning.
3)Assemblies contain MSIL code.
4)Assemblies are Self-Describing. [e.g. metadata,manifest]
5)An assembly is the primary building block of a .NET Framework application.
6)It is a collection of functionality that is built, versioned, and deployed as a single implementation
unit (as one or more files).
7)All managed types and resources are marked either as accessible only within their
implementation unit, or by code outside that unit
What is manifest?
Assembly metadata is stored in Manifest.Manifest contains all the metadata needed to do the
following things
1.Version of assembly
2.Security identity
3.Scope of the assembly
4.resolve references to resources and classes.
5The assembly manifest can be stored in either a PE file (an .exe or .dll) with
Microsoft intermediate language (MSIL) code or in a stand-alone PE file that
contains only assembly manifest information.
Strong name is similar to GUID(It is supposed to be unique in space and time) in COM
components.Strong Name is only needed when we need to deploy assembly in GAC.Strong
Names helps GAC to differentiate between two versions.Strong names use public key
cryptography(PKC) to ensure that no one can spoof it.
If you want to view a assembly how t o you go about it (Twist : what is ILDASM?)?
Ildasm basically converts the whole exe or Dll in tit IL code . to Run ILDASM you hav to go to
C:\program files\ Microsoft visual studio.net 2003\sdk\v1.1\bin.
The gacutil.exe that ships with .NET can be used to add or remove a shared
assembly from the GAC.
gacutil.exe /i myassembly.dll
gacutil.exe /u myassembly.dll
When we talk about the Assembly then the first thing comes into our mind is the
security for high level development. Delayed signing is the terminology when we are
certifying the assembly which will prevent hi-jacking of that assembly.
Delayed signing refers to a technique of partially signing assemblies while they are in
development phase. So, signing an assembly basically certifies that assembly by the
manufacturer and prevents tampering and hi-jacking of that assembly. This is
achievable by using public key/private key encoding of parts of the assembly. The
public key is embedded in the assembly and will be used by third-parties who want
to reference the assembly. There are many more benefits to signing an assembly,
but the main purpose of delayed signing is to allow a company to protect and control
its private key and only use it during the packaging process. A delayed signed
assembly can still be used like a signed assembly, you just can't package and ship it.
sn -k keypair.snk
1. sn -p keypair.snk public.snk
2. Build assembly with:
[assembly: AssemblyDelaySign("false")]
[assembly: AssemblyKeyFile("..\\..\\keypair.snk")]
3. sn -Vr AssemblyName.dll
4. This step is critical and is not mentioned anywhere. Exit and restart every
instance of VisualStudio running. Until you do this Visual Studio will not know
of the sn -Vr from step 4 and you will get
"COM Interop registration failed. The check of the signature failed for
assembly AssemblyName.dll"
What is reflection?
Refelction is the mechanism of discovering class information solely at run
time.Wondering where it would be useful? Imagine,you are in visual studio IDE
(Integrated devolopment environment) and as you type "object." you would see all
the methods,properties and events associated with that object.An other example
would be an Object browser.
pre jit : convert source code to native code in single completion of cycle.Normally
this can be done at the time of deployment.
econo jit : coverts the only called methods to native code,however it can be removed
when are not required.
normal jit : compliled the called methods to native code.In the the methods can be
compiles for the first time.For latter calls it can be displayed using cached items.
Value types
Value types include the following:
• Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte,
Reference Types
• String
• Delegates
Boxing:
means converting value-type to reference-type.
Eg:
int I = 20;
string s = I.ToSting();
UnBoxing:
means converting reference-type to value-type.
Eg:
int I = 20;
string s = I.ToString(); //Box the int
int J = Convert.ToInt32(s); //UnBox it back to an int.
Note:
Performance Overheads due to boxing and unboxing as the boxing makes a copy of
value type from stack and place it inside an object of type System.Object in the heap.
C#.net