根据条件,根据字段值设置字段值或者获取字段值

/// <summary>
/// 按条件修改字段值
/// </summary>
/// <param name="featureLayer">输入图层对象</param>
/// <param name="whereFieldName">条件字段</param>
/// <param name="whereValue">条件字段值</param>
/// <param name="fieldName">修改字段</param>
/// <param name="value">修改字段值</param>
/// <returns></returns>
public static bool SetValueByWhere(IFeatureLayer featureLayer, string whereFieldName, string whereValue, string fieldName, string value)
{
if (featureLayer == null)
{
return false;
}
try
{
string strWhereClause = string.Empty;
strWhereClause = whereFieldName + " = '" + whereValue + "'";
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = strWhereClause;
IFeatureCursor pFeatureCursor = featureLayer.FeatureClass.Search(pQueryFilter, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
if (pFeature.get_Value(pFeature.Fields.FindField(whereFieldName)).ToString() == whereValue)
{
pFeature.set_Value(pFeature.Fields.FindField(fieldName), value);
pFeature.Store();
}
pFeature = pFeatureCursor.NextFeature();
}
return true;
}
catch (Exception ms)
{
throw new Exception(ms.Message);
}
}

/// <summary>
/// 按条件获得字段值
/// </summary>
/// <param name="featureLayer">图层对象</param>
/// <param name="whereFieldName">条件字段名称</param>
/// <param name="whereValue">条件字段值</param>
/// <param name="fieldName">取值字段名称</param>
/// <returns>字段值列表</returns>
public static List<object> GetValueByWhere(IFeatureLayer featureLayer, string whereFieldName, string whereValue, string fieldName)
{
try
{
if (featureLayer == null)
{
return new List<object>();
}
List<object> li = new List<object>();
string strWhereClause = string.Empty;
strWhereClause = whereFieldName + " = '" + whereValue + "'";
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = strWhereClause;
IFeatureCursor pFeatureCursor = featureLayer.FeatureClass.Search(pQueryFilter, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
if (pFeature.get_Value(pFeature.Fields.FindField(whereFieldName)).ToString() == whereValue)
{
li.Add(pFeature.get_Value(pFeature.Fields.FindField(fieldName)));
}
pFeature = pFeatureCursor.NextFeature();
}
return li;
}
catch (Exception)
{
return new List<object>();
}
}

posted @ 2022-03-28 11:46  南山种豆8  阅读(142)  评论(0)    收藏  举报