Getting Started with the Data Table Object

The DataTable object represents tabular data as rows, columns, and constraints. Use the DataTable object to hold data in memory while performing disconnected data operations. The DataTable object can be explicitly created by instantiating the Data-Table class, adding DataColumn objects that define the data to be held, and then adding DataRow objects, which are objects that contain the data. The DataTable object must contain DataColumn objects before any data can be added to the DataTable object. The DataColumn objects also contain constraints, which maintain data integrity by limiting the data that can be placed into a column. The following code creates an employee DataTable and adds DataColumn objects:

Private Function GetDataTable() As DataTable 'Create the DataTable named "Employee" Dim employee As New DataTable("Employee")

'Add the DataColumn using all properties Dim eid As New DataColumn("Eid") eid.DataType = GetType(String) eid.MaxLength = 10 eid.Unique = True eid.AllowDBNull = False eid.Caption = "EID" employee.Columns.Add(eid)

'Add the DataColumn using defaults

Dim firstName As New DataColumn("FirstName")

firstName.MaxLength = 35

firstName.AllowDBNull = False employee.Columns.Add(firstName)

Dim lastName As New DataColumn("LastName")

lastName.All owDBNull = False empl oyee .Col umns .Add(lastName)

'Add the decimal DataColumn using defaults

Dim salary As New DataColumn("Salary", GetType(Decimal))

salary.DefaultValue = 0.0

empl oyee .Col umns .Add (salary)

'Derived column using expression

Dim lastNameFirstName As New DataColumn("LastName and FirstName") lastNameFirstName.DataType = GetType(String) lastNameFirstName.MaxLength = 70

lastNameFirstName.Expression = "lastName + ', ' + firstName" empl oyee .Col umns .Add(lastNameFirstName)

Return employee End Function

private DataTable GetDataTable() {

//Create the DataTable named "employee" DataTable employee = new DataTable("Employee");

//Add the DataColumn using all properties DataColumn eid = new DataColumn("Eid"); eid.DataType = typeof(string); eid.MaxLength = 10; eid.Unique = true; eid.AllowDBNull = false; eid.Caption = "EID"; employee.Columns.Add(eid);

//Add the DataColumn using defaults

DataColumn firstName = new DataColumn("FirstName");

firstName.MaxLength = 35;

firstName.AllowDBNull = false;

empl oyee.Columns.Add(firstName);

DataColumn lastName = new DataColumn("LastName");

lastName.All owDBNull = false;

empl oyee .Col umns .Add(lastName);

//Add the decimal DataColumn using defaults

DataColumn salary = new DataColumn("Salary", typeof(decimal)); salary.DefaultValue = 0.00m; empl oyee.Columns.Add(salary);

//Derived column using expression

DataColumn lastNameFirstName = new DataColumn("LastName and FirstName"); lastNameFirstName.DataType = typeof(string); lastNameFirstName.MaxLength = 70;

lastNameFirstName.Expression = "lastName + ', ' + firstName"; employee.Columns.Add(lastNameFirstName);

return employee;

In this example, the DataType is a string for all DataColumn objects except salary, which is a decimal object that contains currency. The MaxLength property constrains the length of string data. The string data is truncated if you exceed this length and no exception is thrown. If the Unique property is set to true, an index is created to prevent duplication of entries. The AllowDBNull property is set to false to mandate the population of the column with data. The Caption property is a string that holds the column heading that is to be displayed when this DataTable object is used with Web server controls. The lastNameFirstName DataColumn object shows how an expression column is created, in this case, by assigning an expression. Expression columns are also known as calculated or derived columns. Adding a derived column is especially beneficial when data is available but not in the correct format.

The following is a list of the default values for DataColumn properties if you create a DataColumn without specifying a value for a property:

■ DataType String

■ MaxLength -1, which means that no maximum length check is performed

■ Unique False, which allows duplicate values

■ AllowDBNull True, which means that the DataColumn does not need to have a value

■ Caption The DataColumn object, which is the ColumnName property value

Was this article helpful?

0 0
App Gangster

App Gangster

Get All The Support And Guidance You Need To Be A Success At Dominating Apps. This Book Is One Of The Most Valuable Resources In The World When It Comes To Becoming The Godfather Of Smart Phone Apps.

Get My Free Ebook


Post a comment