zoukankan      html  css  js  c++  java
  • DataTable.PrimaryKey 属性

    第一个示例显示如何返回在 DataGrid 中显示的 DataTable 的主键列。第二个示例演示如何为 DataTable 设置主键列。

    Visual Basic
    Private Sub GetPrimaryKeys(table As DataTable)
    ' Create the array for the columns.
    Dim columns() As DataColumn
    columns = table.PrimaryKey
    ' Get the number of elements in the array.
    Console.WriteLine("Column Count: " & columns.Length.ToString())
    Dim i As Integer
    For i = 0 To columns.GetUpperBound(0)
    Console.WriteLine(columns(i).ColumnName & columns(i).DataType.ToString())
    Next i
    End Sub
    Private Sub SetPrimaryKeys()
    ' Create a new DataTable and set two DataColumn objects as primary keys.
    Dim table As DataTable = new DataTable()
    Dim keys(2) As DataColumn
    Dim column  As DataColumn
    ' 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
    End Sub
    
    C#
    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;
    }
  • 相关阅读:
    WPF DataGrid ListView等控件Binding LINQ数据源
    WPF自定义命令
    vb.net与FLASH的完美结合
    [音乐欣赏]鲍家街43号 汪峰 小鸟
    MSGRID的填充
    听!是谁在唱歌
    学习用的几个英文单词
    [学习日记]三层结构
    有关从文件完整路径中提取文件名的方法
    有关TABCONTROL选项卡的动态选择方法
  • 原文地址:https://www.cnblogs.com/top5/p/1432490.html
Copyright © 2011-2022 走看看