Setting primary keys for a DataTable

Source: Internet
Author: User
Tags count tostring
Gets or sets an array of columns which function as primary keys for the data table.

[Visual Basic]
<Serializable>
Public Property PrimaryKey as DataColumn ()
[C #]
[Serializable]
Public datacolumn[] PrimaryKey {get; set;}
[C + +]
[Serializable]
Public: __property datacolumn* Get_primarykey ();
Public: __property void Set_primarykey (datacolumn*[]);
[JScript]
Public
Serializable
function Get PrimaryKey (): datacolumn[];
Public function set PrimaryKey (datacolumn[]);
Property Value
An array of DataColumn objects.

Exceptions
Exception Type Condition
DataException The key is a foreign key.

Remarks
The primary key of a table must is unique to identify the record in the table. It ' s also possible to have a table with a primary key made up of two or more columns. This occurs can ' t contain enough unique values in a single column. For example, a two column primary key might consist of a "FirstName" and "LastName" column. Because primary keys can be made up to more than one column, the PrimaryKey property consists of an array of DataColumn OB Jects.

Example
[Visual Basic, C #] The "example" shows how to return the primary key columns for a DataTable, displayed in a DataGrid. The second example demonstrates to set the primary key columns for a DataTable.

[Visual Basic]
Private Sub Getprimarykeys (myTable as DataTable)
' Create the array for the columns.
Dim Colarr () as DataColumn
Colarr = Mytable.primarykey
' Get the # of elements in the array.
Console.WriteLine ("Column Count:" & ColArr.Length.ToString ())
Dim I as Integer
For i = 0 to Colarr.getupperbound (0)
Console.WriteLine (Colarr (i). ColumnName & Colarr (i). Datatype.tostring ())
Next I
End Sub

Private Sub Setprimarykeys ()
' Create a new DataTable and set two DataColumn objects as primary keys.
Dim myTable as DataTable = new DataTable ()
Dim keys (2) as DataColumn
Dim MyColumn as DataColumn
' Create column 1.
MyColumn = New DataColumn ()
Mycolumn.datatype = System.Type.GetType ("System.String")
Mycolumn.columnname= "FirstName"
' Add the column to the DataTable.Columns collection.
MYTABLE.COLUMNS.ADD (MyColumn)
' Add the column to the array.
Keys (0) = MyColumn

' Create column 2 and add it to the array.
MyColumn = New DataColumn ()
Mycolumn.datatype = System.Type.GetType ("System.String")
Mycolumn.columnname = "LastName"
MYTABLE.COLUMNS.ADD (MyColumn)
' Add the column to the array.
Keys (1) = MyColumn
' Set the Primarykeys property to the array.
Mytable.primarykey = Keys
End Sub
[C #]
private void Getprimarykeys (DataTable myTable) {
Create the array for the columns.
Datacolumn[] Colarr;
Colarr = Mytable.primarykey;
Get the number of elements in the array.
Console.WriteLine ("Column Count:" + colarr.length);
for (int i = 0; i < colarr.length; i++) {
Console.WriteLine (Colarr[i]. ColumnName + colarr[i]. DataType);
}
}

private void Setprimarykeys () {
Create a new DataTable and set two DataColumn objects as primary keys.
DataTable myTable = new DataTable ();
Datacolumn[] keys = new datacolumn[2];
DataColumn MyColumn;
Create column 1.
MyColumn = new DataColumn ();
Mycolumn.datatype = System.Type.GetType ("System.String");
Mycolumn.columnname= "FirstName";
ADD the column to the DataTable.Columns collection.
MYTABLE.COLUMNS.ADD (MyColumn);
ADD the column to the array.
Keys[0] = MyColumn;

Create column 2 and add it to the array.
MyColumn = new DataColumn ();
Mycolumn.datatype = System.Type.GetType ("System.String");
Mycolumn.columnname = "LastName";
MYTABLE.COLUMNS.ADD (MyColumn);
ADD the column to the array.
KEYS[1] = MyColumn;
Set the Primarykeys property to the array.
Mytable.primarykey = keys;
}
[C + +, JScript] No example is available for C + + or JScript. To view a Visual Basic or C # example, click the Language Filter button in the upper-left corner of the page.

Requirements
Platforms:windows, Windows NT 4.0, Windows Millennium Edition, Windows xp-based, Windows XP Home Edition, Windows XP Profe Ssional, Windows. NET Server Family


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.