C#数据表保存表(无限嵌套) [英] C# data table to hold tables (infinite nesting)

查看:135
本文介绍了C#数据表保存表(无限嵌套)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相对较新的 C# - 但是来自一个 C / C ++ 背景。



我需要一个与 DataTable 类似的数据类型(类),但允许存储的列保存简单类型int,float,boolean,[string])AS WELL作为相同类型的数据(以便列可以容纳另一个表,该表还具有存储表等的列)。



在C ++的说法中,我所描述的是以下几点:

  typedef union {/ * ... * / } 值类型; 
typedef std :: vector< ValueType> ColumnValues;

类列
{
private:
std :: string m_name;
ColumnValues m_values;

public:
列(const std :: string& name);
// ...
}

class Table
{
private:
std :: string m_name;
std :: vector< Column> m_cols;

public:
表(const std :: string& name,const std :: vector< Column> * cols_ptr = NULL);
// ...
};

列可以保存任何有效的数据类型 - 其中包括一个表数据类型(因此隐含的无限嵌套能力)。



我最初的想法是继承自 DataTable - 但是以为我会在这里检查是否:


  1. 如果这样的类已经存在某处(在$ code> .Net 图书馆或其他地方)

  2. 如果这是正确的方式

我将需要滚动我自己的事件,我会感激一些指针(即代码片段)来帮助我开始。





建议的用法:我打算使用这种数据类型来主要用于序列化我的库中的对象(所以我可以在消息包中发送它们),但是,所以我可以使用它们作为一种数据字典,允许我存储任意复杂的数据。

解决方案

我不是确定如果我正确理解,但是阻止您创建一个 DataColumn DataTable ,因为它是类型? / p>

如下所示:

  DataTable myTableOfTables = new DataTable( TableOfTables); 
myTableOfTables.Columns.Add(new DataColumn(Table,typeof(DataTable)));

现在您可以继续执行以下操作:



[pre> myTableOfTables.Rows.Add(myTableOfTables.NewRow()[0] = new DataTable(NestedTable));

如果您登录immeadiate窗口,确定:

 ? myTableOfTables.Rows [0] [0] 

你会得到:

  {NestedTable} 
base {System.ComponentModel.MarshalByValueComponent}:{NestedTable}
CaseSensitive:false
ChildRelations:{System .Data.DataRelationCollection.DataTableRelationCollection}
列:{System.Data.DataColumnCollection}
约束:{System.Data.ConstraintCollection}
DataSet:null
DefaultView:{System.Data .DataView}
DisplayExpression:
ExtendedProperties:Count = 0
HasErrors:false
IsInitialized:true
区域设置:{es-ES}
MinimumCapacity :$ 50
名称空间:
ParentRelations:{System.Data.DataRelationCollection.DataTableRelationCollection}
前缀:
PrimaryKey:{System.Data.DataColumn [0]}
RemotingFormat:Xml
行:{System.Data.DataRowCollection}
站点:null
TableName:NestedTable

所以你已经有一个类允许这种结构: DataTable



如果你想要的是一个 DataColumn ,它能够包含任何值,不管它是一个 int ,一个字符串或者你有什么,然后简单地将其类型定义为对象


I'm relatively new to C# - but come from a C/C++ background.

I need a data type (class) that is similar to DataTable, but allows the stored columns to hold "simple" types (int, float, boolean, [string]) AS WELL as data of the same type (so that a column could hold another table which also has columns that stores tables etc).

In C++ parlance, what I am describing is something along these lines:

typedef union { /*... */ } ValueType;
typedef std::vector<ValueType> ColumnValues; 

class Column
{
    private:
       std::string m_name ;
       ColumnValues m_values;

    public:
       Column(const std::string& name);
       // ...
}

class Table
{
   private:
      std::string m_name;
      std::vector<Column> m_cols;

    public:
       Table(const std::string& name, const std::vector<Column> *cols_ptr = NULL);
       // ...   
};

A column can hold any valid data type - which includes a Table data type (hence the implicit infinite nesting capability).

My initial thought approach was to inherit from DataTable - but thought I'd com in here to check if:

  1. If such a class already exists somewhere (either in the .Net library or elsewhere)
  2. if that is the correct way of going about it

In the event that I will need to "roll my own", I would appreciate some pointers (i.e. code snippets) to help me get started.

[Edit]

Proposed usage: I intend to use this data type primarily to serialize objects in my library (so I can send them in message packets etc), but also, so I can use them as a kind of data dictionary that allows me to store data of arbitrary complexity.

解决方案

I am not sure if I'm understanding corrrectly, but what is preventing you from creating a DataColumn with DataTable as it's type?

Something like the following:

DataTable myTableOfTables = new DataTable("TableOfTables);
myTableOfTables.Columns.Add(new DataColumn("Table", typeof(DataTable)));

Now you can go ahead and do the following:

myTableOfTables.Rows.Add(myTableOfTables.NewRow()[0] = new DataTable("NestedTable"));

And sure enough if you check in the immeadiate window:

? myTableOfTables.Rows[0][0]

You will get:

{NestedTable}
    base {System.ComponentModel.MarshalByValueComponent}: {NestedTable}
    CaseSensitive: false
    ChildRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
    Columns: {System.Data.DataColumnCollection}
    Constraints: {System.Data.ConstraintCollection}
    DataSet: null
    DefaultView: {System.Data.DataView}
    DisplayExpression: ""
    ExtendedProperties: Count = 0
    HasErrors: false
    IsInitialized: true
    Locale: {es-ES}
    MinimumCapacity: 50
    Namespace: ""
    ParentRelations: {System.Data.DataRelationCollection.DataTableRelationCollection}
    Prefix: ""
    PrimaryKey: {System.Data.DataColumn[0]}
    RemotingFormat: Xml
    Rows: {System.Data.DataRowCollection}
    Site: null
    TableName: "NestedTable"

So you already have a class that allows this kind of structure: DataTable

If what you want is one DataColumn that is able to contain whatever value, be it an int, a string or what have you, then simply define its type as object.

这篇关于C#数据表保存表(无限嵌套)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆