我有一个程序,从数据库中读取约2百万行到列表。 每一行都是包含诸如地理坐标等信息的位置。C#通用列表foreach OutofMemoryException
一旦数据被添加到我使用foreach循环,抓住坐标创建KML文件列表。当行数很大时,循环会遇到OutOfMemoryException错误(但在其他情况下完美工作)。
如何处理这使程序能够以非常大的数据集的工作有什么建议? kml库是SharpKML。
我还是C#的新手,所以请放轻松!
这是循环:
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
using (cmd)
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
double lat = reader.GetDouble(1);
double lon = reader.GetDouble(2);
string country = reader.GetString(3);
string county = reader.GetString(4);
double TIV = reader.GetDouble(5);
double cnpshare = reader.GetDouble(6);
double locshare = reader.GetDouble(7);
//Add results to list
results.Add(new data(lat, lon, country, county, TIV, cnpshare, locshare));
}
reader.Close();
}
conn.Close();
}
int count = results.Count();
Console.WriteLine("number of rows in results = " + count.ToString());
//This code segment generates the kml point plot
Document doc = new Document();
try
{
foreach (data l in results)
{
Point point = new Point();
point.Coordinate = new Vector(l.lat, l.lon);
Placemark placemark = new Placemark();
placemark.Geometry = point;
placemark.Name = Convert.ToString(l.tiv);
doc.AddFeature(placemark);
}
}
catch(OutOfMemoryException e)
{
throw e;
}
这是在列表
public class data
{
public double lat { get; set; }
public double lon { get; set; }
public string country { get; set; }
public string county { get; set; }
public double tiv { get; set; }
public double cnpshare { get; set; }
public double locshare { get; set; }
public data(double lat, double lon, string country, string county, double tiv, double cnpshare,
double locshare)
{
this.lat = lat;
this.lon = lon;
this.country = country;
this.county = county;
this.tiv = tiv;
this.cnpshare = cnpshare;
this.locshare = locshare;
}
}
为什么地球上你需要所有200万行内存? – vcsjones