0

我有一个场景,我需要使用不太可能改变但有助于实现业务逻辑的表格数据:

Table

 Parent      Child         Relationship      Allowed
  A            B                 C            True
  B            A                 C            False

每行代表一个业务规则。基于此表,我需要使用“关系”列的内容填充下拉控件。我可以在 C# 中使用某种数据结构来存储此表格数据,还是需要使用数据库表?

4

3 回答 3

3

Unless you need to persist this data across sessions, you don't need to store it in a database table. From the perspective of Object Oriented design, why not make an object, and therefore class, that represents the structure you need. Something like this:

public class Relationship
{
    public string Name { get; set; }
    public string Parent { get; set; }
    public string Child { get; set; }
    public bool Allowed { get; set; }
}

Note of course that I'm using strings where you might want to use further objects of their own 'type'. Additionally, keep in mind access, and what should and shouldn't be allowed to access these properties... This is example is intentionally simple at this point!

于 2013-07-11T14:50:08.270 回答
1

我同意,如果这是您需要存储的所有数据,那么一个完整的数据库可能会有点矫枉过正,因此如果您真的想的话,理论上您可以在 C# 中硬编码数据和结构,但在几乎所有情况下都不是一个好主意 - 甚至如果它不太可能改变。

至少将数据存储在一个小的 XML / config 文件中,这样如果它确实发生了变化,您就不需要重新编译应用程序。

于 2013-07-11T14:49:30.967 回答
0

使用数据表:http: //msdn.microsoft.com/en-us/library/System.Data.DataTable.aspx

您可以创建表格并像这样填充它:

// Definition
DataTable table = new DataTable();

DataColumn column = new DataColumn("Parent", typeof(string));
table.Columns.Add(column);

// Data
DataRow row = table.NewRow();
row["Parent"] = "A";
table.Rows.Add(row);
于 2013-07-11T14:52:35.007 回答