Monday, July 30, 2007

ADO.NET 2.0 – Part 2

In the previous articles I have discussed 8 new features of ADO.NET 2.0, following are the remaining.

9. DataReader’s New Execute Methods
Now command object supports more execute methods. Besides old ExecuteNonQuery, ExecuteReader, ExecuteScaler, and ExecuteXmlReader, the new execute methods are ExecutePageReader, ExecuteResultSet, and ExecuteRow.
Figure 2 shows all of the execute methods supported by the command object in ADO.NET 2.0.


Figure 2. Command's Execute methods.

10. Improved Performance for DataSet Remoting
If we think about ADO.NET 1.x DataSet, the major problem which is DataSet Serialization. Microsoft has worked lots on this part and they have improved the performance of Serialization a lot. In ADO.NET 1.x, Serialization of DataSet will happen in XML format. Even in ADO.NET 2.0, by default it happens in XML format. But there is an option to change the Serialization format to Binary using property called "SerializationFormat". Look at the following code.

Dim format As New Binary.BinaryFormatter
Dim ds As New DataSet ds = DataGridView1.DataSource
Using fs As New FileStream("c:\sar1.bin", FileMode.CreateNew)
ds.RemotingFormat = SerializationFormat.Binary
'Other option is SerilaizationFormat.XML
format.Serialize(fs, ds)
End Using

In the above code snippet, we are serializing the dataset into filestream. If we look at the file size difference between XML and Binary formating, XML formating is more than three times bigger than Binary formating. If we see the perfomance of Remoting of DataSet when greater than 1000 rows, the binary formating is 80 times faster than XML formating.

11. DataSet and DataReader Transfer
In ADO.NET 2.0, we can load DataReader directly into DataSet or DataTable. Similarly we can get DataReader back from DataSet or DataTable. DataTable is now having most of the methods of DataSet. For example, WriteXML or ReadXML methods are now available in DataTable also. A new method "Load" is available in DataSet and DataTable, using which we can load DataReader into DataSet/DataTable. In other way, DataSet and DataTable is having method named "getDataReader" which will return DataReader back from DataTable/DataSet. Even we can transfer between DataTable and DataView. Check out the following example,

Dim dr As SqlDataReader
Dim conn As New SqlConnection(Conn_str)
conn.Open()
Dim sqlc As New SqlCommand("Select * from Orders", conn)
dr = sqlc.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As New DataTable("Orders")
dt.Load(dr)

12. Batch Updates
In previous versions of ADO.NET, if we do changes to DataSet and update using DataAdapter.update method. It makes round trips to datasource for each modified rows in DataSet. This fine with few records, but if there is more than 100 records in modified. Then it will make 100 calls from DataAccess layer to DataBase which is not acceptable. In this release, MicroSoft have changed this behaiour by exposing one property called "UpdateBatchSize". Using this we can metion how we want to groups the rows in dataset for single hit to database. For example if we want to group 50 records per hit, then we need to mention "UpdateBatchSize" as 50.

13. Common Provider Model
In our application if want to implement provider independent DataAccess, then we need to write our own factory classes for returning the required objects like connection, command. And for implementing this feature only provider independent interface were available in the previous releases. But in ADO.NET 2.0, we have separate factory classes for managing common provider model. A new class "DbProviderFactory" is included in this release which has two methods. One method "GetFactoryClasses" to get all the provider installed in that machine and other one "GetFactory" will be used to get provider specific object by providing provider name as paramter.

Check out the following example, in which without knowing which provider we are going to work on we are fetching values from database. We need to pass only "Providername" which can configurable and which can change.

Dim pf As DbProviderFactory pf = DbProviderFactories.GetFactory(providername)
Using dbc As DbConnection = pf.CreateConnection
dbc.ConnectionString = Conn_str dbc.Open()
Dim comm As DbCommand = dbc.CreateCommand
comm.CommandText = "Select * from orders"
Dim dr As DbDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection)
Dim ldt As New DataTable("Orders") l
dt.Load(dr)
End Using

14. Bulk Copy
If we think of bulk copy i.e. if we want to move some data from one datasource to another datasource. If will simply think of doing this in database, since we dont have much options in the previous release. But in ADO.NET 2.0, we can do this from DataAccess Layer itself.
New class called "SQLBulkCopy" is included in this release which will do this work for us. Using this class we can metion which datasource we want to copy and to which destination table we want to copy. We can even map the columns between tables, by default it will copy columns to columns. Check out the following example,

Dim dr As SqlDataReader
Dim conn As New SqlConnection(Conn_str)
Dim conn1 As New SqlConnection(Conn_str1)
conn.Open()
conn1.Open()
Dim sqlc As New SqlCommand("Select * from Orders", conn)
'dr = sqlc.ExecutePageReader(CommandBehavior.CloseConnection, 10, 10)

dr = sqlc.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As New DataTable("Orders")
Dim bulkcopy As New SqlBulkCopy(conn1)
bulkcopy.DestinationTableName = "MVPOrders"
bulkcopy.WriteToServer(dr)

15. Multiple Active ResultSets
Using this feature we can have more than one simultaneous pending request per connection i.e. multiple active datareader is possible. Previously when a DataReader is open and if we use that connection in another datareader, we used to get the following error "Systerm.InvalidOperationException: There is already an open DataReader associated with this connection which must be closed first". This error wont come now, as this is possible now because of MAR's. This feature is supported only in Yukon.

16. Conclusion
ADO.NET 2.0 provides many new and improved features for developers to improve the performance and reduce the code. In this article, I discussed top 15 features of ADO.NET 2.0

Wednesday, July 18, 2007

ADO.NET 2.0 - Part 1

Following is the new Features of ADO.NET 2.0


1. Data Paging
Custom paging is one of the major requirements in ASP.NET. Similarly if we take windows application also, paging is an important feature that is required. In previous releases, we need to write stored procedure for doing paging in our applications.

But in ADO.NET, we can do is very simply. An new API "ExecutePageReader" in SQLCommand will do all the stuff for we and return only the required records. This method is very similar to ExecuteReader but it will accept two extra parameter. One is "Starting row number" and other one is for "number of rows". This will also return datareader. For example, check out the following code snippet.


Dim dr As SqlDataReader

Dim conn As New SqlConnection(Conn_str)

conn.Open()

Dim sqlc As New SqlCommand("Select * from Orders", conn)

dr = sqlc.ExecutePageReader(CommandBehavior.CloseConnection, 10, 10)

2. Asynchronous Data Access
In ADO.NET 1.x commands like ExecuteReader,ExecuteScalar and ExecuteNonQuery will synchronously execute and block the current thread. Even when we open connection to the database, current thread is blocked. But in ADO.NET 2.0, all of these methods comes with Begin and End methods to support asynchronous execution.

This asynchrounous methodology is very similar to our .NET framework asynchronous methodology. Even we can have callback mechanism using this approach.

This Asynchrounous Data Access is currently only supported in SQLClient, but complete API support is available for other providers to implement this mechanism.

3. Connection Details
Now we can get more details about a connection by setting Connection's StatisticsEnabled property to True. The Connection object provides two new methods - RetrieveStatistics and ResetStatistics. The RetrieveStatistics method returns a HashTable object filled with the information about the connection such as data transferred, user details, curser details, buffer information and transactions.

4. DataSet, RemotingFormat Property
When DataSet.RemotingFormat is set to binary, the DataSet is serialized in binary format instead of XML tagged format, which improves the performance of serialization and deserialization operations significantly.

5. DataTable’s Load and Save Methods
In previous version of ADO.NET, only DataSet had Load and Save methods. The Load method can load data from objects such as XML into a DataSet object and Save method saves the data to a persistent media. Now DataTable also supports these two methods.
We can also load a DataReader object into a DataTable by using the Load method.

6. New Data Controls
In Toolbox, we will see these new controls - DataGridView, DataConnector, and DataNavigator. See Figure 1. Now using these controls, we can provide navigation (paging) support to the data in data bound controls.


Figure 1. Data bound controls.

7. DbProvidersFactories Class
This class provides a list of available data providers on a machine. We can use this class and its members to find out the best suited data provider for database when writing a database independent applications.

8. Customized Data Provider
By providing the factory classes now ADO.NET extends its support to custom data provider. Now we don't have to write a data provider dependent code. We use the base classes of data provider and let the connection string does the trick for we.
Ohter features will be released in next article.

Wednesday, July 4, 2007

C++ and Java

Comparision of C++ and Java

Advantages Of C++
Each computer language has a niche which it is known for. C++ boasts object oriented programming which is very segmented, easy to work with, and doesn't require very many lines of code to perform simple tasks. Although C++ is backwards-compatible with its predecessor, the C language, C is not object oriented while C++ is.

C++ is perhaps one of the easiest computer languages to learn as much of the syntax is very straight-forward. In fact, it is often taught in many college classrooms as a first language for Computer Science majors.

The language is not to be underestimated, however, as it is still extremely flexible and functional in the workforce.Although C++ is a high-level language, it is very powerful in that it allows the programmer benefits otherwise only available in the assembly (low-level) language.

For example, programmers have much control over memory management, as can be demonstrated with arrays and linked lists.Yet another benefit of C++ is its ability to handle OOP, or object oriented programming.

By using functions and what are known as classes, certain parts of the code may be re-used multiple times throughout the program. For example, suppose a function was written to add two numbers being passed into it, and to print out the result.

This function can be re-used multiple times by passing in two different numbers, each time.Perhaps one of the most important advantages to C++, however, is its ability to work in cross-platform environments. This is because of an ANSI standard.

In other words, C++ code can be used to develop programs for vast operating systems including MS-DOS, Windows, Macintosh, UNIX and Linux, to name just a few. Unfortunately, GUI (graphical user interface) development in C++ among operating systems varies greatly.

Microsoft Visual C++, for example, allows for graphics in Windows. QT, meanwhile, can be used on UNIX-based machines.

Advantages Of Java
Java is a fairly new language which has been developed to improvise on C++. Unlike C++, it is completely object oriented.
The use of classes in development is not optional.Java also boasts easier to implement pointers than C++. Linked lists are extremely easy to develop.
In addition, many methods (functions) for almost everything you could imagine are pre-defined.One of the biggest advantages of Java over C++ is that GUI development is cross-platform. T
he exact same code can be run on virtually any operating system. For this reason, Java is a viable solution for many web-based applications.