此方法通过IFeatureBuffer将一个SHP文件中所有的Feature及属性添加到SDE中对应的FeatureClass中.
#region 添加SHP数据到SDE数据方法二
/// <summary>
/// 添加SHP数据到SDE数据方法二
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void add02_Click(object sender, EventArgs e)
{
//调用方法获取SHP文件中的要素集
IFeatureClass featureClass = GetShpFeatureClass();
IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilter();
//根据SHP文件中的要素集逐一插入到SDE对应要素集中
for (int i = 0; i < featureClass.FeatureCount(queryFilter); i++)
{
IFeature shpFeatureBuffer = featureClass.GetFeature(i);
Add_Fea("roadtest", shpFeatureBuffer);
}
}
/// <summary>
/// 获得shp文件里的要素集
/// </summary>
/// <returns></returns>
public IFeatureClass GetShpFeatureClass()
{
IFeatureClass returnFeatureClass = null;//返回要素集
string filepath = Server.MapPath("./") + "temp";
//打开shp文件工作空间
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pFeatWS = pWorkspaceFactory.OpenFromFile(@filepath, 0) as IFeatureWorkspace;
//获取SHP文件的要素集
returnFeatureClass = pFeatWS.OpenFeatureClass("road03.shp");
return returnFeatureClass;//返回要素集
}
/// <summary>
/// 将Feature添加到SDE指定的FeatureClass里
/// </summary>
/// <param name="FeaName">SDE要素集名称 ,此处为roadtest</param>
/// <param name="insertFeature">插入的要素</param>
public void Add_Fea(string FeaName, IFeature insertFeature)
{
//打开SDE工作空间
IPropertySet propSet = new PropertySetClass();
propSet.SetProperty("Server", "GZGISSER");
propSet.SetProperty("Instance", "5151");
propSet.SetProperty("Database", "gzgddb");
propSet.SetProperty("user", "sde");
propSet.SetProperty("password", "gzgissersde");
propSet.SetProperty("version", "SDE.DEFAULT");
IWorkspaceFactory pWorkSpFac = new SdeWorkspaceFactoryClass();
IFeatureWorkspace pFeaWorkSp = null;
pFeaWorkSp = (IFeatureWorkspace)(pWorkSpFac.Open(propSet, 0));//打开要素空间
IFeatureClass FeaCls = pFeaWorkSp.OpenFeatureClass(FeaName);//取得要素集
///插入要素到要素集中
IFeatureCursor FeaCursor = FeaCls.Insert(true);
IFeatureBuffer FeaBuffer = FeaCls.CreateFeatureBuffer(); ;
//获取对应要素的字段将其插入到SDE要素集中
IField Fld = new FieldClass();
IFields Flds = insertFeature.Fields;
for (int i = 0; i < Flds.FieldCount; i++)
{
//获取对应字段的索引
Fld = Flds.get_Field(i);
int index = FeaBuffer.Fields.FindField(Fld.Name);
if (index != -1)
{
//字段赋值
FeaBuffer.set_Value(index, insertFeature.get_Value(i));
}
}
//插入要素
FeaCursor.InsertFeature(FeaBuffer);
//清空工作空间
pFeaWorkSp = null;
}
#endregion