Ado. NET object Construction (4)

Source: Internet
Author: User
Tags expression xml attribute
Ado| Object DataColumn Construction

When n public Sub new () is newly created, the new DataColumn object has no default ColumnName or Caption property. However, when you add to DataColumnCollection, you give the column the default name ("Column1", "Column2", and so on).

N Public Sub New (ByVal columnName as String)

N Public Sub New (ByVal columnName as String,byval DataType as Type)

N Public Sub New (ByVal columnName as String,byval DataType as Type,byval expr as String)

N Public Sub New (ByVal columnName as String,byval DataType as Type,byval expr as String,byval Type as MappingType)

Parameters

1. ColumnName A string that represents the name of the column to be created. If set to a null reference (Nothing in Visual Basic) or an empty string (""), a default name is provided when added to the column collection.

2. DataType supported by DataType.

3. Expr is used to create the expression for the column.

4. Type MappingType one of the values. Specifies how DataColumn is mapped. When you get or set the DataColumn ColumnMapping property, use the MappingType enumeration. When the dataset is raised using the WriteXml method to write data and schemas as XML documents, this property determines how the values of the columns are written.

Member name
Description

Attribute
Maps a column to an XML attribute.

Element
Maps a column to an XML element.

Hidden
Maps a column to an internal structure.

simplecontent
Maps a column to a XmlText node.



DataColumn is the basic building block for the schema used to create a DataTable. This schema is generated by adding one or more DataColumn objects to the datacolumncollection.

Each DataColumn has a DataType attribute that determines the kind of data that DataColumn contains. For example, you can limit the data type to integers, strings, or decimals. Because the data contained in a DataTable is typically merged back into its original data source, you must match the data type with the data type in the data source.

Attributes such as AllowDBNull, Unique, and ReadOnly help ensure data integrity by imposing restrictions on data entry and update. You can also use the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties to control the automatic generation of data.

You can also make sure that the values in DataColumn are unique by creating uniqueconstraint and adding them to the constraintcollection of the DataTable to which DataColumn belongs.

To create a relationship between DataColumn objects, create a DataRelation object and add it to the datarelationcollection of the DataSet.

You can use the Expression property of the DataColumn object to calculate values in a column or to create an aggregation column.

Example
Private Sub Createcomputedcolumn (ByVal myTable as DataTable)

Dim myTable as DataTable = new DataTable ("MyTable")
Dim MyColumn as DataColumn

Dim DT as System.Type

Dim strexpr as String

MyColumn = New DataColumn ("id")

With MyColumn

. DataType = System.Type.GetType ("System.Int32")

. AutoIncrement = True

. AutoIncrementSeed = 1

. AutoIncrementStep = 1

. ReadOnly = True

End With

MYTABLE.COLUMNS.ADD (MyColumn)



DT = System.Type.GetType ("System.Int32")

MyColumn = New DataColumn ("Quantity", DT)

With MyColumn

. AllowDBNull = False

End With

MYTABLE.COLUMNS.ADD (MyColumn)



MyColumn = New DataColumn
Mycolumn.datatype = System.Type.GetType ("System.Decimal")
Mycolumn.allowdbnull = False
Mycolumn.caption = "Price"
Mycolumn.columnname = "Price"
Mycolumn.defaultvalue = 25
MYTABLE.COLUMNS.ADD (MyColumn)


DT = System.Type.GetType ("System.Decimal")

MyColumn = New DataColumn ("Tax", DT, "Price *. 0862")

With MyColumn

. AutoIncrement = False

. ReadOnly = True

End With

MYTABLE.COLUMNS.ADD (MyColumn)



DT = System.Type.GetType ("System.Decimal")

strexpr = "Price * Quantity"

' Create ' column, setting the type to Attribute.

MyColumn = New DataColumn ("Total", DT, strexpr, MappingType.Attribute)

Mycolumn.autoincrement = False

Mycolumn.readonly = True

MYTABLE.COLUMNS.ADD (MyColumn)

End Sub



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.