我有 2 个这样的对象:
public class Place : ICloneable, IEquatable<Place>
{
public string Name{ get; set; }
public float longitude{ get; set; }
public float latitude{ get; set; }
public Horizon thehorizon { get; set; }
...
}
public class Horizon
{
public List<PointF> points{ get; set; }
}
在我的数据库中,我有 2 个表 - “地点”和“视野”,视野中有一个外键,可以知道这些点属于哪个地方。
所以这些地方的结构是:
- 名称 -nvarchar - 主键
- 经度 - 实数
- 纬度 - 真实
视野的结构是
- parent_key - nvarchar
- pointX - 实数
- poinY - 真实的
我编写了下面的代码来选择所有数据并构建一个地点列表。它正在工作,但速度很慢。如果您对如何使其更快(或任何评论)有任何建议,请告诉我。
DataTable TablePlaces;
DataTable TableHorizons;
public void init()
{
TablePlaces = new DataTable();
TablePlaces.Columns.Add("Name");
TablePlaces.Columns.Add("longitude");
TablePlaces.Columns.Add("latitude");
TableHorizons = new DataTable();
TableHorizons.Columns.Add("parent_key");
TableHorizons.Columns.Add("pointX");
TableHorizons.Columns.Add("pointY");
System.Data.DataSet DS = new DataSet();
DS.Tables.Add(TablePlaces);
DS.Tables.Add(TableHorizons);
DS.Relations.Add(TablePlaces.Columns["Name"],
TableHorizons.Columns["parent_key"]);
}
public List<Place> BuilsListPlace()
{
TableHorizons.Clear();
TablePlaces.Clear();
using (DbCommand Command = newConnectionNewCommand())
{
Command.CommandText = "SELECT * FROM places ORDER BY Name"
fill(TablePlaces, Command);
Command.CommandText = "SELECT * FROM horizons ORDER BY parent_key,pointX";
fill(TableHorizons, Command);
Command.Connection.Dispose();
}
return (from DataRow dr in TablePlaces.Rows
select newPlace(dr)).ToList();
}
void fill(TableDB t ,DbCommand Command)
{
using (var da = newDataAdapter())
{
da.SelectCommand = Command;
da.MissingSchemaAction = MissingSchemaAction.Ignore;
da.Fill(t);
}
}
Place newPlace(DataRow dr)
{
Place result = new Place();
result.longitude=(float)dr["longitude"];
result.latitude=(float)dr["latitude"];
result.Name=(string)dr["Name"];
result.theHorizon=newHorizon(dr.GetChildRows(dr.Table.ChildRelations[0]));
return result;
}
Horizon newHorizon(DataRow[] Rows)
{
Horizon result = new Horizon();
result.points = new List<PointF>();
foreach(DataRow dr in Rows)
result.points.Add(new PointF((float)dr["pointX"],(float)dr["pointY"]);
return result;
}