以下を使用して、DBクエリからオブジェクトを作成しています。
public static T Query<T>(this MySqlConnection conn, string query) where T : new()
{
T obj = default(T);
conn.Open();
using (MySqlCommand command = new MySqlCommand(query, conn))
{
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
obj = new T();
PropertyInfo[] propertyInfos;
propertyInfos = typeof(T).GetProperties();
for (int i = 0; i < reader.FieldCount; i++)
{
var name = reader.GetName(i);
foreach (var item in propertyInfos)
{
if (item.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) && item.CanWrite)
{
item.SetValue(obj, reader[i], null);
}
}
}
}
}
}
conn.Dispose();
return obj;
}
私はこれとlinqの使用などのためにそこにあるライブラリを知っています。しかし、ここで私が本当に求めているのは、上記の効果的な逆です。オブジェクトを渡してデータベースに挿入したいのですが、完全に理解できません。
私は以下を持っていますが、挿入クエリを構築するためのオブジェクトの値を取得できません:
public static T Insert<T>(this MySqlConnection conn) where T : new()
{
string query = "INSERT INTO " + typeof(T).Name;
string fields = "(";
string values = "(";
T obj = default(T);
conn.Open();
using (MySqlCommand command = conn.CreateCommand())
{
obj = new T();
PropertyInfo[] propertyInfos;
propertyInfos = typeof(T).GetProperties();
command.CommandText = query;
command.ExecuteNonQuery();
}
conn.Close();
return obj;
}
感謝します。渡されたオブジェクトからビルドされた挿入クエリが必要です。最初の部分は、プロパティテーブルから選択するために、以下のクラスで正常に機能します。
public class Property : ManagedObject
{
private int id; //(most sites the property will have a unique ID)
private string address;
private string city;
private string state;
private string zipCode;
private string propertyUrl;
private string status; //Status of the Property (most have that option, and it either says “in contract” or “price reduced” (most sites will have this don’t worry about the one that don’t)
private Propertytype type;
private double price;
private double squareFeet;
private DateTime listDate; //(date it first appeared on the site or if one is given, that date)
private DateTime yearBuilt;
private List<PriceDrop> priceDrops;
public Property()
{
priceDrops = new List<PriceDrop>();
}
public int ID
{
get { return this.id; }
set
{
this.CheckPropertyChanged<int>
("ID", ref this.id, ref value);
}
}
public string Address
{
get { return this.address; }
set
{
this.CheckPropertyChanged<string>
("Address", ref this.address, ref value);
}
}
public string City
{
get { return this.city; }
set
{
this.CheckPropertyChanged<string>
("City", ref this.city, ref value);
}
}
public string State
{
get { return this.state; }
set
{
this.CheckPropertyChanged<string>
("State", ref this.state, ref value);
}
}
public string ZipCode
{
get { return this.zipCode; }
set
{
this.CheckPropertyChanged<string>
("ZipCode", ref this.zipCode, ref value);
}
}
public string PropertyUrl
{
get { return this.propertyUrl; }
set
{
this.CheckPropertyChanged<string>
("PropertyUrl", ref this.propertyUrl, ref value);
}
}
public string Status
{
get { return this.status; }
set
{
this.CheckPropertyChanged<string>
("Status", ref this.status, ref value);
}
}
public Propertytype Type
{
get { return this.type; }
set
{
this.CheckPropertyChanged<Propertytype>
("Type", ref this.type, ref value);
}
}
public double Price
{
get { return this.price; }
set
{
this.CheckPropertyChanged<double>
("Price", ref this.price, ref value);
}
}
public double SquareFeet
{
get { return this.squareFeet; }
set
{
this.CheckPropertyChanged<double>
("SquareFeet", ref this.squareFeet, ref value);
}
}
public DateTime ListDate
{
get { return this.listDate; }
set
{
this.CheckPropertyChanged<DateTime>
("ListDate", ref this.listDate, ref value);
}
}
public DateTime YearBuilt
{
get { return this.yearBuilt; }
set
{
this.CheckPropertyChanged<DateTime>
("YearBuilt", ref this.yearBuilt, ref value);
}
}
public List<PriceDrop> PriceDrops
{
get { return this.priceDrops; }
set
{
this.CheckPropertyChanged<List<PriceDrop>>
("PriceDrops", ref this.priceDrops, ref value);
}
}
}
var query = ObjectFactory.Query<Property>(AppSettings.Instance.MySqlConn, "SELECT * FROM Property");
今、私はインサートが必要です。