0 sur 1 ont trouvé cela utile - Évaluez ce sujet

DataTable.PrimaryKey, propriété

Obtient ou définit un tableau de colonnes qui fonctionnent comme des clés primaires pour la table de données.

Espace de noms : System.Data
Assembly : System.Data (dans system.data.dll)

public DataColumn[] PrimaryKey { get; set; }
/** @property */
public DataColumn[] get_PrimaryKey ()

/** @property */
public void set_PrimaryKey (DataColumn[] value)

public function get PrimaryKey () : DataColumn[]

public function set PrimaryKey (value : DataColumn[])

Valeur de la propriété

Tableau d'objets DataColumn.
Type d'exceptionCondition

DataException

Il s'agit d'une clé étrangère.

La clé primaire d'une table doit être unique pour identifier l'enregistrement dans la table. Il est également possible qu'une table possède une clé primaire constituée de plusieurs colonnes. Cela se produit si une seule colonne ne peut pas contenir suffisamment de valeurs uniques. Par exemple, une clé primaire à deux colonnes peut être constituée d'une colonne "FirstName" et d'une colonne "LastName". Comme les clés primaires peuvent être composées de plusieurs colonnes, la propriété PrimaryKey comprend un tableau d'objets DataColumn.

Le premier exemple illustre la manière de retourner les colonnes de clés primaires d'un DataTable affiché dans un DataGrid. Le deuxième exemple illustre la définition des colonnes de clés primaires de DataTable.

private void GetPrimaryKeys(DataTable table)
{
    // Create the array for the columns.
    DataColumn[] columns;
    columns = table.PrimaryKey;

    // Get the number of elements in the array.
    Console.WriteLine("Column Count: " + columns.Length);
    for(int i = 0; i < columns.Length; i++)
    {
        Console.WriteLine(columns[i].ColumnName + columns[i].DataType);
    }
}
 
private void SetPrimaryKeys()
{
    // Create a new DataTable and set two DataColumn objects as primary keys.
    DataTable table = new DataTable();
    DataColumn[] keys = new DataColumn[2];
    DataColumn column;

    // Create column 1.
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.ColumnName= "FirstName";

    // Add the column to the DataTable.Columns collection.
    table.Columns.Add(column);

    // Add the column to the array.
    keys[0] = column;
 
    // Create column 2 and add it to the array.
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.ColumnName = "LastName";
    table.Columns.Add(column);

    // Add the column to the array.
    keys[1] = column;

    // Set the PrimaryKeys property to the array.
    table.PrimaryKey = keys;
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition

Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.

.NET Framework

Prise en charge dans : 2.0, 1.1, 1.0

.NET Compact Framework

Prise en charge dans : 2.0, 1.0
Cela vous a-t-il été utile ?
(1500 caractères restants)

Ajouts de la communauté

AJOUTER
© 2013 Microsoft. Tous droits réservés.