Typed DataSet
As all of us know, we can specify the Data type when we create a DataColumn for a DataTable. This is to enforce the runtime Type-safety for the column so that only data of specified data type can be stored in the column. In the same way, in most cases, we prefer to make a DataSet itself Type-safe to protect it from runtime mismatch. Hence Typed DataSets generate classes that expose each object in the DataSet in a Type-safe manner. These classes are inherited directly from the DataSet class.
Let us look into a small example that explains a Typed DataSet.
1. Using DataSet
// Create SqlDataAdapter
SqlDataAdapter daEmp = new SqlDataAdapter("SELECT empno, empname, empaddress FROM EMPLOYEE", conn);
// Create a DataSet object
DataSet dsEmp = new DataSet();
// Fill the DataSet
daEmp.Fill(dsEmp, "EMPLOYEE");
// Print first row and first column (assuming empno is the first column)
Console.WriteLine(dsEmp.Tables["EMPLOYEE"].Rows[0][0].ToString());
// Attempt to assign a string value to the first column (will cause runtime error)
try
{
dsEmp.Tables["EMPLOYEE"].Rows[0][0] = "12345";
Console.WriteLine("Value successfully assigned (This won't be printed)");
}
catch (Exception ex)
{
Console.WriteLine("Error assigning string to integer column: " + ex.Message);
}
If we observe the above code we will get a runtime error when this code gets executed as the value assigned to the column (empno) does not take string value. Also, any misspelling of the column will generate a runtime error. And also we need to go through the hierarchy to get the final value.
2. Using Typed DataSet
// Create SqlDataAdapter
SqlDataAdapter daEmp = new SqlDataAdapter("SELECT empno, empname, empaddress FROM EMPLOYEE", conn);
// Create a strongly-typed DataSet object (assuming EmployeeDS has a table named EMPLOYEE)
EmployeeDS dsEmp = new EmployeeDS();
// Fill the DataSet
daEmp.Fill(dsEmp, "EMPLOYEE");
// Print first row and first column (assuming empno is the first column)
Console.WriteLine(dsEmp.EMPLOYEE[0].empno.ToString());
// Attempt to assign a string value to the first column (will cause compile-time error)
dsEmp.EMPLOYEE[0].empno = "12345"; // Compile-time error: cannot assign string to int
If we see the above code, a typed dataset is very much similar to a normal dataset. But the only difference is that the schema is already present for the same. Hence any mismatch in the column will generate compile-time errors rather than runtime errors as in the case of a normal dataset. Also accessing the column value is much easier than the normal dataset as the column definition will be available in the schema.
How to Generate a Typed DataSet?
Let's create typed DataSet using Visual Studio IDE.
Creating a Typed DataSet in Visual Studio
Let me explain a step-by-step procedure to create a Typed DataSet in Visual Studio.
- Open VS .Net IDE Click on File -> New -> Project and Select Console Application.
- Enter the name of the project. Say TypedDataSetTest.
- Right-click on the solution and click on Add-> Add New Item will show a dialog box.
Select DataSet from the templates pane, give the name (Say TypedDs.xsd), and click on Open. This will add a file by the name TypedDs.xsd to the solution.
- Click on the Server Explorer browse to the database and drop the table on the TypedDs.xsd file.
If we check the XML file for the same then we can see the schema for the table.
There is also a corresponding DataSet class created in the project. This DataSet class and its respective XSD files have the definitions of database columns and their types. This Data can be used in the same manner as the normal dataset to get the data.
Continue here: Working with DataSet and Its Methods in ADO.NET using C#