C# 如何从数据库中读取动态属性
我会尽力以最好的方式解释我的情况。我的数据库中有以下表格:C# 如何从数据库中读取动态属性,c#,ado.net,data-access-layer,sqldatareader,C#,Ado.net,Data Access Layer,Sqldatareader,我会尽力以最好的方式解释我的情况。我的数据库中有以下表格: Products{ProductId, CategoryId ...} Categories{CategoryId ...} CategoryProperties{CategoryPropertyId, CategoryId, Name ...} PropertyValues{ProductId, CategoryPropertyId, Value ...} 因此,我们的目标是拥有属于某个类别的产品列表,并且每个类别可以有“n”个属性
Products{ProductId, CategoryId ...}
Categories{CategoryId ...}
CategoryProperties{CategoryPropertyId, CategoryId, Name ...}
PropertyValues{ProductId, CategoryPropertyId, Value ...}
因此,我们的目标是拥有属于某个类别的产品列表,并且每个类别可以有“n”个属性,这些属性的值在“PropertyValues”表中。我有一个基于“categoryId”返回数据的查询,因此我可以始终从数据库中获得不同的结果:
Products{ProductId, CategoryId ...}
Categories{CategoryId ...}
CategoryProperties{CategoryPropertyId, CategoryId, Name ...}
PropertyValues{ProductId, CategoryPropertyId, Value ...}
对于CPU类别:
intel i7 | CPU | Model | Socket | L1 Cash | L2 Cahs | etc.
samsung F3 | HDD | Model | Speed | GB | etc.
对于HDD类别:
intel i7 | CPU | Model | Socket | L1 Cash | L2 Cahs | etc.
samsung F3 | HDD | Model | Speed | GB | etc.
因此,根据查询中的类别,我总是可以得到不同的列号和名称。对于数据库访问,我使用对返回结果的存储过程的简单ADO.NET调用。
但由于查询结果本质上是动态的,我不知道什么是读取这些数据的好方法
我制作了一个产品
实体,但我不知道如何制作:(
我想我可以制作一个产品
实体,也可以制作其他继承产品
的实体,比如Cpu
,Hdd
,Camera
,PcCase
,GraphicalCard
,MobilePhone
,Gps
等等。但我认为这很愚蠢使用我可以在域中以200+个实体结束此操作。
在这种情况下你会怎么做?
如何读取此动态属性以及将其放置在何处?
更新-一些解决方案
好吧,基于@millimoose对
字典的建议和@Tim Schmelter使用DataTable
对象的想法,我找到了一些解决方案。
现在…这样做可以读取数据并显示它们。
但是我仍然需要比我聪明的人给我一些建议,比如我做得好吗,或者我应该更好地处理这个问题,或者我应该做一些spageti代码。
我所做的是:
public class Product
{
public Product()
{
this.DynamicProperties = new List<Dictionary<string, string>>();
}
public List<Dictionary<string, string>> DynamicProperties { get; set; }
}
...
List<Product> products = new List<Product>();
...
using (SqlDataAdapter a = new SqlDataAdapter(cmd))
{
DataTable t = new DataTable();
a.Fill(t);
Product p = null;
foreach (DataRow row in t.Rows)
{
p = new Product();
foreach (DataColumn col in t.Columns)
{
string property = col.ColumnName.ToString();
string propertyValue = row[col.ColumnName].ToString();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(property, propertyValue);
p.DynamicProperties.Add(dictionary);
}
products.Add(p);
}
}
公共类产品
{
公共产品()
{
this.DynamicProperties=新列表();
}
公共列表动态属性{get;set;}
}
...
列表产品=新列表();
...
使用(SqlDataAdapter a=newsqldataadapter(cmd))
{
DataTable t=新的DataTable();
a、 填充(t);
乘积p=null;
foreach(t.Rows中的数据行)
{
p=新产品();
foreach(t.Columns中的数据列col)
{
string属性=col.ColumnName.ToString();
string propertyValue=行[col.ColumnName].ToString();
字典=新字典();
添加(属性,propertyValue);
p、 DynamicProperties.Add(字典);
}
产品.加入(p);;
}
}
您有一个产品,一组类别,每个类别都有一组属性
class Product
{
public int Id { get; set; }
public Category Category { get; set; }
public List<ProductProperty> Properties { get; set; }
}
class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
class ProductProperty
{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
或者如果它的名称/值
Properties.Add(new ProductionProperty() { Name="Speed", Value="150MB/s"});
你最初的方法
public class Product
{
public List<Dictionary<string, string>> DynamicProperties { get; set; }
}
有些相关的问题:是否有任何理由需要在模型类中提供这些属性,而不仅仅是一个产品
一个字典
(或类似的东西)?最简单的方法是使用DataTables
。然后你只需要一个sql查询和SqlDataAdapter
来填充这个表。@millimoose我一开始想的是Dictionary
。但是我不知道这个场景有什么不同的解决方案。或者Dictionary
实际上是唯一或最好的解决方案。@1110看来你是在要求一条安全毯。你试着使用字典
,这似乎是一个显而易见的选择,如果这导致你的设计出现问题,回来问问这个问题?@1110你也可以看看Steve Yegge的帖子,关于他所说的。免责声明:时间太长了,他有脱离ra的倾向每个帖子可能不止一次ils。我不能像这样处理它。属性
属性应该是动态的,所以我不能读取它,因为我不知道属性的名称。它是“动态的”,您只需使用返回的内容填充每个ProductProperty