0
一般的なSQLエグゼキュータをbuidしようとしています。SqlDataReaderコンテキストで属性を使用する方法は?
私のDTOクラスの属性が私のSQLテーブルの列と同じ名前を持っていれば動作しています。
私のコードの一般的な部分を増やすために、自分のDTO属性をSQL列に分離するためにDTOに属性を追加します。
しかし、それは
を働いていない私のDTOクラス:public class CarModels
{
[DbColumn("ca_id")] //column name
public string Id { get; set; }
[DbColumn("ca_label")] //column name
public string Label { get; set; }
}
私の一般的な方法:
public List<T> ExecuteSQLSELECTCommand<T>(string SQLcommand) where T : new()
{
IDictionary<string, string> databaseMappings = new Dictionary<string, string>(); ;
Get_Connection();
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection = connection;
cmd.CommandText = string.Format(SQLcommand);
List<T> res = new List<T>();
try
{
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
T t = new T();
for (int inc = 0; inc < reader.FieldCount; inc++)
{
Type type = t.GetType();
//how to get attribute link to current FiedCount ?
PropertyInfo prop = type.GetProperty(reader.GetName(inc));
prop.SetValue(t, reader.GetValue(inc), null);
}
res.Add(t);
}
reader.Close();
}
catch (Exception e)
{
}
return res;
}
}
と私のコール:
List<CarModels> carTest = db.ExecuteSQLCommand<CarModels>("SELECT ca_id, ca_label from cartracker.ca_cars");
私の質問、どのようにすることができます私はattの価値を回復するMySqlDataReaderコンテキストでPropertyInfoを構築するにはどうすればいいですか?
ありがとうございました。