Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datatable单元格是否可以包含datatable?_C#_Ado.net - Fatal编程技术网

C# datatable单元格是否可以包含datatable?

C# datatable单元格是否可以包含datatable?,c#,ado.net,C#,Ado.net,如果不是,我应该使用哪种结构?数据集可以包含多个具有不同模式的数据表。您可以将第二个数据表存储在第二个表中,并将其引用到第一个表中的行 还可以使用和方法将数据表存储到数据单元中 :看一看 假设您打算将其持久化到数据库中,最好查看外键/DataRelation。如果您确实希望单元格中包含更多结构化数据,那么可能需要xml或其他序列化格式。根据我从您的问题中推断的,您可以这样说: DataRow dr = new DataRow(); DataTable dtable = dr[0].Table

如果不是,我应该使用哪种结构?

数据集可以包含多个具有不同模式的数据表。您可以将第二个数据表存储在第二个表中,并将其引用到第一个表中的行

还可以使用和方法将数据表存储到数据单元中


:看一看

假设您打算将其持久化到数据库中,最好查看外键/
DataRelation
。如果您确实希望单元格中包含更多结构化数据,那么可能需要xml或其他序列化格式。

根据我从您的问题中推断的,您可以这样说:

DataRow dr = new DataRow();
DataTable dtable = dr[0].Table
MSDN:DataRow的DataTable属性:获取此行具有架构的DataTable


示例和说明:

您可以将新的
DataTable
添加到
DataTable
单元格中。 简单的例子:

使用一个单元格类型定义主表
DataTable

DataTable people = new DataTable();
people.Columns.Add("Name", typeof(string));
people.Columns.Add("Friends", typeof(DataTable));
DataTable output = (DataTable)people.Rows[0]["Friends"];
定义子表:

DataTable friends = new DataTable();
friends.Columns.Add("Name", typeof(string));
friends.Columns.Add("Desire", typeof(string));
friends.Rows.Add("Scarecrow", "brain");
friends.Rows.Add("Tin Woodman", "heart");
friends.Rows.Add("Cowardly Lion", "courage");
向子表添加一些数据:

DataTable friends = new DataTable();
friends.Columns.Add("Name", typeof(string));
friends.Columns.Add("Desire", typeof(string));
friends.Rows.Add("Scarecrow", "brain");
friends.Rows.Add("Tin Woodman", "heart");
friends.Rows.Add("Cowardly Lion", "courage");
最后,向主表添加一行:

people.Rows.Add("Dorothy", friends);
要从主表获取子表,需要将对象强制转换为
DataTable

DataTable people = new DataTable();
people.Columns.Add("Name", typeof(string));
people.Columns.Add("Friends", typeof(DataTable));
DataTable output = (DataTable)people.Rows[0]["Friends"];

我可能错了,但我相信OP是指嵌套数据,而不是从一行返回到表中。这就是我所知道的:)您只需在此处找到相关信息:-1:您无法写入
DataRow dr=new DataRow()
。我相信DataRow的ctor是.Net内部的,只能由
DataTable.NewRow()
之类的人访问,它返回一个新的
DataRow
,引用所属表的架构。从答案中可以看出,人们很难理解您的意思。请详细说明你的问题。显示您正在尝试做什么的伪代码将有所帮助。@Alfred我完全同意您的观点,当人们不详细阐述他们的问题并等待其他人理解他们的假设时,这确实让我感到困扰。算了吧,我们不是算命的,不是吗。我想要嵌套的数据表。必须阅读答案。(或使用辅助键值表)