关键是在之前要填充这些信息
myCommand.FillSchema(ds,SchemaType.Mapped,"myTable");
myCommand.Fill(ds,"myTable");DataColumn[] objColumn;
objColumn = ds.Tables["myTable"].PrimaryKey;
for (int i = 0; i <= objColumn.Length - 1; i++)
{
    Label3.Text += (objColumn[i].ColumnName + ";");
}

解决方案 »

  1.   

    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;
     }