C# }; } ... var brands=CreateListOfItems(tblbrand、CreateItemBrandFromDBData);
我编写了一个方法,该方法获取从存储过程检索到的结果,并将其转换为对象列表。 您唯一需要注意的是: 要映射到的类的列名必须与从数据库发送的列名相同。 public IEnumerable <T> ExecuteReaderToList<T>(string storedProcedure, IDictionary parameters = null, string connectionString = null) { ICollection list = new List();C# }; } ... var brands=CreateListOfItems(tblbrand、CreateItemBrandFromDBData);,c#,reflection,collections,C#,Reflection,Collections,我编写了一个方法,该方法获取从存储过程检索到的结果,并将其转换为对象列表。 您唯一需要注意的是: 要映射到的类的列名必须与从数据库发送的列名相同。 public IEnumerable <T> ExecuteReaderToList<T>(string storedProcedure, IDictionary parameters = null, string connectionString = null) {
var properties = typeof(T).GetProperties();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = storedProcedure;
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (KeyValuePair<string, object> parameter in parameters)
{
cmd.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
cmd.Connection = conn;
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var element = Activator.CreateInstance<T>();
foreach (var f in properties)
{
var o = reader[f.Name];
if (o.GetType() != typeof(DBNull))
{
f.SetValue(element, o, null);
}
o = null;
}
list.Add(element);
}
}
conn.Close();
}
return list;
}
public IEnumerable ExecuteReaderToList(字符串存储过程,IDictionary参数=null,
字符串连接字符串=null)
{
ICollection list=新列表()
var-properties=typeof(T).GetProperties();
使用(SqlConnection conn=newsqlconnection(connectionString))
{
SqlCommand cmd=新的SqlCommand();
cmd.CommandText=存储过程;
cmd.CommandType=CommandType.storedProcess;
if(参数!=null)
{
foreach(参数中的KeyValuePair参数)
{
cmd.Parameters.AddWithValue(parameter.Key,parameter.Value);
}
}
cmd.Connection=conn;
conn.Open();
使用(SqlDataReader=cmd.ExecuteReader())
{
while(reader.Read())
{
var element=Activator.CreateInstance();
foreach(属性中的var f)
{
var o=读取器[f.Name];
if(o.GetType()!=typeof(DBNull))
{
f、 设置值(元素,o,null);
}
o=零;
}
列表。添加(元素);
}
}
康涅狄格州关闭();
}
退货清单;
}
我编写了一个方法,该方法获取从存储过程检索到的结果,并将其转换为对象列表。 您唯一需要注意的是: 要映射到的类的列名必须与从数据库发送的列名相同。 public IEnumerable <T> ExecuteReaderToList<T>(string storedProcedure, IDictionary parameters = null, string connectionString = null) { ICollection list = new List();
var properties = typeof(T).GetProperties();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = storedProcedure;
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (KeyValuePair<string, object> parameter in parameters)
{
cmd.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
cmd.Connection = conn;
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var element = Activator.CreateInstance<T>();
foreach (var f in properties)
{
var o = reader[f.Name];
if (o.GetType() != typeof(DBNull))
{
f.SetValue(element, o, null);
}
o = null;
}
list.Add(element);
}
}
conn.Close();
}
return list;
}
public IEnumerable ExecuteReaderToList(字符串存储过程,IDictionary参数=null,
字符串连接字符串=null)
{
ICollection list=新列表()
var-properties=typeof(T).GetProperties();
使用(SqlConnection conn=newsqlconnection(connectionString))
{
SqlCommand cmd=新的SqlCommand();
cmd.CommandText=存储过程;
cmd.CommandType=CommandType.storedProcess;
if(参数!=null)
{
foreach(参数中的KeyValuePair参数)
{
cmd.Parameters.AddWithValue(parameter.Key,parameter.Value);
}
}
cmd.Connection=conn;
conn.Open();
使用(SqlDataReader=cmd.ExecuteReader())
{
while(reader.Read())
{
var element=Activator.CreateInstance();
foreach(属性中的var f)
{
var o=读取器[f.Name];
if(o.GetType()!=typeof(DBNull))
{
f、 设置值(元素,o,null);
}
o=零;
}
列表。添加(元素);
}
}
康涅狄格州关闭();
}
退货清单;
}
这里的问题是什么?显然,您已经创建了一个
列表
,问题是如何从数据库中的一行构造T
的实例?您实际的问题是什么?您可以创建对象列表(list
)。@LasseV.Karlsen感谢您的回复。是的,从表中检索所有行,然后将其存储在我的列表中。请澄清您的实际问题。您知道如何从数据库中执行的语句中检索行吗?您知道如何构造T
的实例吗?你知道如何通过反射为实例分配属性值吗?@LasseV.Karlsen我已经更新了我的问题。谢谢这里有什么问题?显然,您已经创建了一个列表
,问题是如何从数据库中的一行构造T
的实例?您实际的问题是什么?您可以创建对象列表(list
)。@LasseV.Karlsen感谢您的回复。是的,从表中检索所有行,然后将其存储在我的列表中。请澄清您的实际问题。您知道如何从数据库中执行的语句中检索行吗?您知道如何构造T
的实例吗?你知道如何通过反射为实例分配属性值吗?@LasseV.Karlsen我已经更新了我的问题。谢谢这里有什么问题?显然,您已经创建了一个列表
,问题是如何从数据库中的一行构造T
的实例?您实际的问题是什么?您可以创建对象列表(list
)。@LasseV.Karlsen感谢您的回复。是的,从表中检索所有行,然后将其存储在我的列表中。请澄清您的实际问题。您知道如何从数据库中执行的语句中检索行吗?您知道如何构造T
的实例吗?你知道如何通过反射为实例分配属性值吗?@LasseV.Karlsen我已经更新了我的问题。谢谢,汉克斯,我会尝试重构你提供的这个片段,看看它是否正常工作。如果你有任何问题,请告诉我。我也有完整的代码从数据库中选择列表,若你们需要的话。若你们想使用像这个列名和实体名一样的通用函数,你们还需要记住一件事。只有当此方法
<code>
// Create a new type of the entity I want
Type t = typeof(T);
T returnObject = new T();
for (int i = 0; i < dataReader.FieldCount; i++) {
string colName = string.Empty;
colName = dataReader.GetName(i);
// Look for the object's property with the columns name, ignore case
PropertyInfo pInfo = t.GetProperty(colName.ToLower(), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
// did we find the property ?
if (pInfo != null) {
if (dataReader.Read()) {
object val = dataReader[colName];
// is this a Nullable<> type
bool IsNullable = (Nullable.GetUnderlyingType(pInfo.PropertyType) != null);
if (IsNullable) {
if (val is System.DBNull) {
val = null;
} else {
// Convert the db type into the T we have in our Nullable<T> type
val = Convert.ChangeType(val, Nullable.GetUnderlyingType(pInfo.PropertyType));
}
} else {
// Convert the db type into the type of the property in our entity
val = Convert.ChangeType(val, pInfo.PropertyType);
}
// Set the value of the property with the value from the db
pInfo.SetValue(returnObject, val, null);
}
}
}
</code>
public IEnumerable<T> CreateListOfItems(string tableName,
Func<MySqlDataReader, T> itemCreator)
{
var items = new List<T>();
using (var strConn = getMySqlConnection())
{
string query = "SELECT * FROM " + tableName;
var cmd = new MySqlCommand(query, strConn);
strConn.Open();
var rd = cmd.ExecuteReader();
while (rd.Read())
{
items.Add(itemCreator(rd));
}
}
return items;
}
private ItemBrand CreateItemBrandFromDBData(MySqlDataReader rd)
{
return new ItemBrand
{
BrandID = Convert.ToInt32(rd["brandID"]),
Name = rd["brandName"].ToString()
};
}
...
var brands = CreateListOfItems<ItemBrand>(tblbrand, CreateItemBrandFromDBData);
public IEnumerable <T> ExecuteReaderToList<T>(string storedProcedure, IDictionary parameters = null,
string connectionString = null)
{
ICollection list = new List();
var properties = typeof(T).GetProperties();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = storedProcedure;
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (KeyValuePair<string, object> parameter in parameters)
{
cmd.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
cmd.Connection = conn;
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var element = Activator.CreateInstance<T>();
foreach (var f in properties)
{
var o = reader[f.Name];
if (o.GetType() != typeof(DBNull))
{
f.SetValue(element, o, null);
}
o = null;
}
list.Add(element);
}
}
conn.Close();
}
return list;
}