2009-12-25 6 views
0

Je tente d'obtenir l'imbrication dans DataTable, c'est-à-dire qu'une colonne de DataTable est un DataTable. Mon code est quelque chose comme ceci:Problème lors de l'imbrication DataTable

DataTable table = new DataTable();
Colonne DataColumn = new DataColumn ("Qualifications", System.Type.GetType ("System.Data.DataTable"));
table.Colonnes.Ajout (colonne);

Je reçois un message d'erreur d'exécution à la ligne 2, qui indique "Column nécessite un DataType valide". Quelle pourrait être la raison?

Répondre

2

Je dirais que ce que vous essayez de réaliser n'est pas possible en utilisant la manière que vous avez spécifiée. Pour établir une relation entre une entité et plusieurs sous-entités, utilisez une relation un-à-plusieurs entre une table et une autre table.

Cela signifie que vous avez deux tables séparées, les appeler par exemple TableOne et TableMany. Dans TableOne mettez tous les champs qui décrivent votre entité, et assurez-vous d'avoir une clé primaire. Dans TableMany mettez tous les champs qui décrivent vos sous-entités, et incluez un champ qui est du type du champ clé primaire TableOne, qui relie chaque sous-entité à son entité propriétaire.

Pour examle:

TableOne: 
    int   PrimaryKey 
    nvarchar(50) Name 

TableMany: 
    int   ForeignKey 
    nvarchar(50) Qualification 
    int   Grade 

TableOne sample content: 
    PrimaryKey  Name 
    1    Alice 
    2    Bob 
    3    Charlie 

TableMany sample content: 
    ForeignKey  Qualification Grade 
    1    Driving   100 
    1    Acting   60 
    1    Singing   30 
    2    Driving   40 
    2    Piloting   90 
    2    Snowboarding  80 
    3    Dancing   70 
    3    Tennis   30 

Exemple de code:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace datatests 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program p = new Program(); 
      p.Main(); 
     } 

     DataSet dataSet; 
     DataTable TableOne, TableMany; 
     DataRelation OneToManyRelation; 

     void Main() 
     { 
      dataSet = new DataSet(); 

      TableOne = new DataTable(); 
      var TableOnePK = TableOne.Columns.Add("PrimaryKey", typeof(int)); 
      TableOne.Columns.Add("Name", typeof(string)); 

      TableMany = new DataTable(); 
      var TableManyFK = TableMany.Columns.Add("ForeignKey", typeof(int)); 
      TableMany.Columns.Add("Qualification", typeof(string)); 
      TableMany.Columns.Add("Grade", typeof(int)); 

      dataSet.Tables.Add(TableOne); 
      dataSet.Tables.Add(TableMany); 

      TableOne.Constraints.Add("PK", TableOnePK, true); 
      OneToManyRelation = new DataRelation("OneToMany", TableOnePK, TableManyFK); 

      TableOne.ChildRelations.Add(OneToManyRelation); 

      // Populate TableOne with sample data. 
      AddTableOneRow(1, "Alice"); 
      AddTableOneRow(2, "Bob"); 
      AddTableOneRow(3, "Charlie"); 

      // Populate TableMany with sample data. 
      AddTableManyRow(1, "Driving", 100); 
      AddTableManyRow(1, "Acting", 60); 
      AddTableManyRow(1, "Singing", 30); 
      AddTableManyRow(2, "Driving", 40); 
      AddTableManyRow(2, "Piloting", 90); 
      AddTableManyRow(2, "Snowboarding", 80); 
      AddTableManyRow(3, "Dancing", 70); 
      AddTableManyRow(3, "Tennis", 30); 

      var parentRow=TableOne.Rows[0]; 
      var childRows = parentRow.GetChildRows(OneToManyRelation); 
      Console.WriteLine("Data for record key #{0}, Name={1}", 
       parentRow["PrimaryKey"], 
       parentRow["Name"]); 
      Console.WriteLine("Qualifications:"); 
      foreach (DataRow childRow in childRows) 
      { 
       Console.WriteLine(" {0}: {1}", 
        childRow["Qualification"], 
        childRow["Grade"]); 
      } 
     } 

     private void AddTableManyRow(int fk, string qual, int grade) 
     { 
      var newRow = TableMany.NewRow(); 
      newRow["ForeignKey"] = fk; 
      newRow["Qualification"] = qual; 
      newRow["Grade"] = grade; 
      TableMany.Rows.Add(newRow); 
     } 

     private void AddTableOneRow(int key, string name) 
     { 
      var newRow = TableOne.NewRow(); 
      newRow["PrimaryKey"] = key; 
      newRow["Name"] = name; 
      TableOne.Rows.Add(newRow); 
     } 
    } 
} 

Exemple de sortie:

Data for record key #1, Name=Alice 
Qualifications: 
    Driving: 100 
    Acting: 60 
    Singing: 30 
+0

explication brillante. Merci. – Kabeer

+0

Vous êtes les bienvenus :) –

Questions connexes