Wednesday, 18 February 2015

Difference between Co-related Sub-Query and Sub-query

Below example is not Co-related Sub-Query. It is Derived Table / Inline-View since i.e, a Sub-query within FROM Clause.

A Corelated Sub-query should refer its parent(main Query) Table in it. For example See find the Nth max salary by Co-related Sub-query:

SELECT Salary
FROM Employee E1
WHERE N-1 = (SELECT COUNT(*)
             FROM Employee E2
             WHERE E1.salary <E2.Salary)
Co-Related Vs Nested-SubQueries.

Technical difference between Normal Sub-query and Co-related sub-query are:

1. Looping: Co-related sub-query loop under main-query; whereas nested not; therefore co-related sub-query executes on each iteration of main query. Whereas in case of Nested-query; subquery executes first then outer query executes next. Hence, the maximum no. of executes are NXM for correlated subquery and N+M for subquery.

2. Dependency(Inner to Outer vs Outer to Inner): In the case of co-related subquery, inner query depends on outer query for processing whereas in normal sub-query, Outer query depends on inner query.

3.Performance: Using Co-related sub-query performance decreases, since, it performs NXM iterations instead of N+M iterations. ¨ Co-related Sub-query Execution.


Saturday, 14 February 2015

Persistence concept in Objects of C#

 What is Persistence concept?

Persistence is the capability of an object to save and restore data. The object can be saved to the disk with its current state. You can use it later from the disk, with the attributes with which it was saved. Without this capability of saving and restoring we would have to re-configure object every time we quit an application.

How Can you do it Programmatically in objects of C# and your own Objects ?

The .NET framework ships with two serialization systems. One is represented by the System.Xml.Serialization.XmlSerializer class and is intended for saving and loading objects to and from human-readable XML. The other is represented by System.Runtime.Serialization.Formatter, and is designed to save and load arbitrary objects in binary or SOAP format (or any other custom formats you may want to provide, but you probably won't need that), mainly for remoting, but useful for saving and loading to and from files, as well. Both are useful in different situations -- if you have a relatively simple object and want to have a lot of control over the format in which it's saved, use XmlSerializer. If you have a complex object (especially if it has recursive object references -- that is, a property that points to an object that has a back-reference to the parent object), and don't really care what format you use, use Formatter (specifically, BinaryFormatter, as it's more concise than SoapFormatter). The Formatter classes can serialize and deserialize any object that has a SerializableAttribute applied to it, or implements ISerializable. 99 out of 100 times, you won't need special support for ISerializable -- just put SerializableAttribute on your class, and it'll usually work (the exception being if your object has members that can't be persisted properly, such as GDI+ objects like Brush or Pen -- if you need this support, post back with some details, and I can give you examples of how to do it). Here's an example of how to use BinaryFormatter (System.Runtime.Serialization.Formatters.Binary.BinaryFormatter) to save and load an object:
public void SerializeObject(object o, string fileName)
{
 using (Stream s = File.OpenWrite(fileName))
 {
  BinaryFormatter bf = new BinaryFormatter();
  bf.Serialize(s, o);
 }
}


public object DeserializeObject(string fileName)
{
 using (Stream s = File.OpenRead(fileName))
 {
  BinaryFormatter bf = new BinaryFormatter();
  return bf.Deserialize(s);
 }
}
XmlSerializer is a bit less robust, in that it is more limited in the objects it's able to handle (for example, it can't handle recursive references or, in fact, any kind of strongly-typed reference to another object in the graph other than a simple parent/child relationship), but it's useful when you need your data to be human-readable (or human-editable), and even more so if you have an already-established XML format that you have to use. If you have an XSD schema for your format, you can use the xsd.exe tool included in the .NET framework to generate a class which the XmlSerializer can use to read and write the XML described in the schema. Here are the same two functions described above, implemented using the XmlSerializer:
public void SerializeObject(object o, string fileName)
{
 using (TextWriter tw = new StreamWriter(File.OpenWrite(fileName)))
 {
  XmlSerializer xs = new XmlSerializer(o.GetType());
  xs.Serialize(tw, o);
 }
}

public object DeserializeObject(string fileName, Type t)
{
 using (TextReader tr = new StreamReader(File.OpenRead(fileName)))
 {
  XmlSerializer xs = new XmlSerializer(t);
  return xs.Deserialize(tr);
 }
}
Notice that the major differences in usage between the two serializers are that Formatter uses Streams while XmlSerializer can use readers and writers (or streams, but it simply creates a reader/writer to wrap around the stream internally), and that Formatter can get all the information it needs from the serialized stream, but XmlSerializer needs to be told the type of the root object.

Saturday, 7 February 2015

What is a CLR?

What Is CLR?

Full form of CLR is Common Language Runtime and it forms the heart of the .NET framework.
All Languages have runtime and it is the responsibility of the runtime to take care of the code
execution of the program. For example, VC++ has MSCRT40.DLL, VB6 has MSVBVM60.DLL,
and Java has Java Virtual Machine etc. Similarly, .NET has CLR. Following are the
responsibilities of CLR
• Garbage Collection: - CLR automatically manages memory thus eliminating
memory leaks. When objects are not referred, GC automatically releases those
memories thus providing efficient memory management.
• Code Access Security: - CAS grants rights to program depending on the security
configuration of the machine. Example the program has rights to edit or create a
new file but the security configuration of machine does not allow the program to
delete a file. CAS will take care that the code runs under the environment of
machines security configuration.
• Code Verification: - This ensures proper code execution and type safety while the
code runs. It prevents the source code to perform illegal operation such as accessing
invalid memory locations etc.
• IL (Intermediate language)-to-native translators and optimizer’s:- CLR uses
JIT, compiles the IL code to machine code, and then executes. CLR also determines
depending on platform what is optimized way of running the IL code.

What is an IL?

What is an IL?

(IL)Intermediate Language is also known as MSIL (Microsoft Intermediate Language) or CIL
(Common Intermediate Language). All .NET source code is compiled to IL. IL is then converted
to machine code at the point where the software is installed, or at run-time by a Just-In-Time
(JIT) compiler.