我正在尝试从地理数据库中提取sdf文件。随着新的sdf文件的创建,程序的内存使用量增加。为了解决这个问题,我尝试重新连接sdf文件,并尝试使用GC释放资源.Collect()方法但问题仍然存在。
/// <summary>
/// Get feature data from geodatabase file
/// </summary>
/// <param name="dictionaryProperty"> Schema of table </param>
/// <param name="table">table connection of gdb file</param>
/// <param name="sdffilepath">sdf file path</param>
static void GetFeatureData(Dictionary<string, Property> dictionaryProperty, Table table, string sdffilepath)
{
int filecount = 1; ;
List<Property> propertyList = new List<Property>();
IEnumerable<string> propertycoll = dictionaryProperty.Select(i => i.Value).Where(i => i.DataType == "OID").ToList().Select(i => i.PropertyName);
string propertyname = null;
foreach (string item in propertycoll)
{
propertyname = item;
}
for (int rowNo = 1; rowNo <= table.RowCount; rowNo++)
{
foreach (Row row in table.Search("*", propertyname + "=" + rowNo.ToString(), RowInstance.Recycle))
{
foreach (Property property in dictionaryProperty.Values)
{
Property objproperty = new Property();
objproperty.PropertyName = property.PropertyName;
objproperty.DataType = property.DataType;
switch (property.DataType)
{
case "SmallInteger":
if (row.IsNull(property.PropertyName))
{
objproperty.PropertyData = "";
}
else
{
objproperty.PropertyData = row.GetShort(property.PropertyName).ToString();
}
break;
case "Geometry":
switch (row.GetGeometry().geometryType.ToString())
{
case "Point":
PointShapeBuffer pointGeometry = row.GetGeometry();
objproperty.GeometryType = property.GeometryType;
objproperty.GeometryData = pointGeometry.point.x + " " + pointGeometry.point.y;
break;
objproperty.GeometryData = coord;
objproperty.PropertyData = coord;
objproperty.GeometryType = property.GeometryType;
coord = string.Empty;
break;
case "Polygon":
break;
}
break;
case "GlobalID":
break;
}
propertyList.Add(objproperty);
}
InsertFeatureData(table, propertyList, sdffilepath);
propertyList.Clear();
}
}
Console.WriteLine(filecount + "file completed");
filecount++;
}
/// <summary>
/// Insert feature data to sdf file path
/// </summary>
/// <param name="table"></param>
/// <param name="propertyList">Schema defination</param>
/// <param name="sdffilepath"></param>
static void InsertFeatureData(Table table, List<Property> propertyList, string sdffilepath)
{
IConnection con = OpenFDOSDFConnection(sdffilepath);
IInsert insertCommand = (IInsert)con.CreateCommand(OSGeo.FDO.Commands.CommandType.CommandType_Insert);
insertCommand.SetFeatureClassName(GetTableName(table));
foreach (Property objProperty in propertyList)
{
switch (objProperty.DataType)
{
case "OID":
if (!string.IsNullOrEmpty(objProperty.PropertyData))
{
insertCommand.PropertyValues.Add(new PropertyValue(objProperty.PropertyName, new Int64Value(Convert.ToInt64(objProperty.PropertyData))));
}
else
{
insertCommand.PropertyValues.Add(new PropertyValue(objProperty.PropertyName, null));
}
break;
case "Geometry":
switch (objProperty.GeometryType)
{
case "Point":
if (!(objProperty.GeometryData == ""))
{
FgfGeometryFactory factory = new FgfGeometryFactory();
DirectPositionCollection pcollection = new DirectPositionCollection();
string[] points = objProperty.GeometryData.Split();
insertCommand.PropertyValues.Add(new PropertyValue("Geometry", new GeometryValue(factory.GetFgf(factory.CreatePoint(factory.CreatePositionXY(Convert.ToDouble(points[0]), Convert.ToDouble(points[1])))))));
}
}
}
insertCommand.Execute();
insertCommand.Dispose();
con.Close();
}