嵌套 DataTable 时出现问题

发布于 2024-08-16 05:33:27 字数 268 浏览 2 评论 0原文

我试图在DataTable中实现嵌套,即DataTable的一列是一个DataTable。我的代码是这样的:

DataTable table = new DataTable();
DataColumn 列 = new DataColumn("资格", System.Type.GetType("System.Data.DataTable"));
表.Columns.Add(列);

我在第 # 2 行收到一条运行时错误消息,内容为“列需要有效的数据类型”。可能是什么原因?

I am trying to achieve nesting in DataTable, i.e. a column of DataTable is a DataTable. My code is something like this:

DataTable table = new DataTable();
DataColumn column = new DataColumn("Qualifications", System.Type.GetType("System.Data.DataTable"));
table.Columns.Add(column);

I am getting a runtime error message at line # 2, that says "Column requires a valid DataType". What could be the reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2024-08-23 05:33:27

我想说的是,使用您指定的方式是不可能实现您想要实现的目标的。要实现一个实体和多个子实体之间的关系,请使用一个表和另一个表之间的一对多关系。

这意味着您有两个单独的表,例如将它们称为 TableOneTableMany。在TableOne中放置描述您的实体的所有字段,并确保有一个主键。在 TableMany 中放置描述子实体的所有字段,并包含一个 TableOne 主键字段类型的字段,该字段将每个子实体与其拥有实体。

例如:

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

示例代码:

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

示例输出:

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

I would say that what you are attempting to achieve is not possible using the way you specified. To achieve a relationship between an entity and several sub-entities use a one-to-many relationship between one table and another table.

This means you have two separate tables, call them for instance TableOne and TableMany. In TableOne put all the fields that describe your entity, and make sure to have a primary key. In TableMany put all the fields that describe your sub-entities, and include a field which is of the type of the TableOne primary key field, which relates each sub-entity to its owning entity.

For 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

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

Sample output:

Data for record key #1, Name=Alice
Qualifications:
    Driving: 100
    Acting: 60
    Singing: 30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文