VB .NET - Typed and Untyped Dataset

by Anonymous | 4:29:00 PM in |

Well, I grab this chance to explain a bit about the difference between Typed and Untyped Dataset in .Net. Typed Dataset sometimes can be called as Strongly Typed Dataset and vice versa for Untyped Dataset (Weakly Typed Dataset).
I made a big mistake in my life when people keep asking me, which one is better, typed or untyped?
My answer was Untyped Dataset without gave them a specific explanation! Till now, I always use Untyped Dataset, because it has become my habit.
And I don't know that is a good habit or not. I put all things into code, those could be maintained easily according to me.
When the schema of dataset or data structure has been changed, we no need to regenerate again the dataset class with each change in the data structure.
Let's dive into clear explanation about dataset

What is a dataset?
Datasets store data in a disconnected cache. The structure of a dataset is similar to that of a relational database; it exposes a hierarchical object model of tables, rows, and columns.
In addition, it contains constraints and relationships defined for the dataset.
You use datasets if you want to work with a set of tables and rows while disconnected from the data source.
One more thing we have to remember is we can create a relationship in dataset, that's cool man!
That's a good feature when we want create a master-detail relationship and put in datagrid especially devexpress's grid control.
Programming burden is become lesser ever!

So what is a Typed Dataset or Strongly Typed Dataset?
A typed dataset is a dataset that is first derived from the base DataSet class and then uses information in an XML Schema file (an .xsd file) to generate a new class.
Information from the schema (tables, columns, and so on) is generated and compiled into this new dataset class as a set of first-class objects and properties.
Visual Studio has more tool support for typed datasets, and they make programming with the dataset easier and less error-prone.
Typed access is not only easier to read, but is fully supported by IntelliSense in the Visual Studio Code Editor. In addition to being easier to work with, the syntax for the typed dataset provides type checking at compile time, greatly reducing the possibility of errors in assigning values to dataset members.
Access to tables and columns in a typed dataset is also slightly faster at run time because access is determined at compile time, not through collections at run time.
Also that if you are getting errors in Typed DataSet, they will most probably be in compile time and not runtime so you can always fix them.
We can create a Typed Dataset using IDE VS, right click on solution properties, choose Add > Dataset.

Assuming that we already added a dataset and some datatables with their relationship eg. Employee and SaleMaster.
Let's see how easy it can refer to the specific column and row that are present in the dataset.

Code:
Dim sEmployeeID As String
Dim sSaleMasterID As String

Dim oTypedDsEmployeeSale As New dsEmployeeSale
sEmployeeID = oTypedDsEmployeeSale.Employee(0).EmployeeID
sSaleMasterID = oTypedDsEmployeeSale.SaleMaster(0).SaleMasterID

The last is we can create a class file from the dataset with extension .xsd.
Using VS Command Prompt we find the path of .xsd file location which you want create. We should use keyword 'xsd', eg. xsd dsEmployeeSale.xsd /c /l:VB
/c is creating a class file from an .xsd file
/l:VB is creating a class file in VB Language, you can also choose C# Language.
For more information about which type of escape sequence you want to apply, you can enter 'xsd /?'
There are many things that you can do depend on your needs.
Below is the screen shots.



Secondly what is a Untyped Dataset or Weakly Typed Dataset?
An untyped dataset, in contrast, has no corresponding built-in schema. As in a typed dataset, an untyped dataset contains tables, columns, and so on — but those are exposed only as collections.
(However, after manually creating the tables and other data elements in an untyped dataset, you can export the dataset's structure as a schema using the dataset's WriteXmlSchema method.)
Even though typed datasets have many advantages, there are a variety of circumstances under which an untyped dataset is useful. The most obvious scenario is that no schema is available for the dataset.
This might occur, for example, if your application is interacting with a component that returns a dataset, but you do not know in advance what its structure is.
Similarly, there are times when you are working with data that does not have a static, predictable structure; in that case, it is impractical to use a typed dataset, because you would have to regenerate the typed dataset class with each change in the data structure.

If we want use an Untyped Dataset, we have to create it with code. As the relationship of its datatables, we also have to put some codes to create it.
So this is really a pure code without depending to designer of IDE.

Code:
Dim sEmployeeID As String
Dim sSaleMasterID As String

Dim oUntypedDsEmployeeSale As New DataSet
oUntypedDsEmployeeSale.Tables.Add("Employee")
oUntypedDsEmployeeSale.Tables.Add("SaleMaster")

Dim primaryKeyColumn As DataColumn = oUntypedDsEmployeeSale.Tables("Employee").Columns("CustomerID")
Dim foreignKeyColumn As DataColumn = oUntypedDsEmployeeSale.Tables("SaleMaster").Columns("SaleMasterID")
oUntypedDsEmployeeSale.Relations.Add("EmployeeSale", primaryKeyColumn, foreignKeyColumn)

sEmployeeID = oUntypedDsEmployeeSale.Tables("Employee").Rows(0).Item("EmployeeID").ToString
sSaleMasterID = oUntypedDsEmployeeSale.Tables("SaleMaster").Rows(0).Item("SaleMasterID").ToString

I still confuse for deciding which type should I use, but I believe that depends on our requirement.
Only this simple explanation which I can share at this moment, happy programming!

0 comments:

Followers

Status

Latest Posts