报错:对 COM 组件的调用返回了错误 HRESULT E_FAIL。
添加字段事件代码:
protected void BtnNewFild_Click(object sender, EventArgs e)
{
if (TxbFild.Text != "")
{ IFeatureClass pFeatureClass = GetFeatureClass(@"F:\贵州省石漠化数据服务平台\区县石漠化数据2005to2015\区县石漠化数据2005to2015.shp");
//获取shp文件里的字段集合
IFields pFields = pFeatureClass.Fields;
int fieldsCount = pFields.FieldCount;
int m = 0;
for (int i = 0; i < fieldsCount; i++)
{
if (TxbFild.Text != (pFields.get_Field(i).Name ))
{
m += 1;
}
else
{
Response.Write("<script>alert('输入的字段名已存在')</script>");
}
}
if (m == fieldsCount)
{
//在新增字段之前,先修改SchemaLock的状态
ISchemaLock pSchemaLock = pFeatureClass as ISchemaLock;
pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);//set and exclusive lock on the class 设置并独占锁
//首先一定要获取Featureclass,利用Featureclass获取IWorkspace和IWorkspaceEdit:
//获取要素类
IWorkspace pWorkspace = ((IDataset)pFeatureClass).Workspace;
//pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(@"F:\贵州省石漠化数据服务平台\区县石漠化数据2005to2015\区县石漠化数据2005to2015.shp"), 0);//System.IO.Path.GetDirectoryName(filePath)作用是获取shp文件所在的文件夹目录
IWorkspaceEdit workspaceEdit = pWorkspace as IWorkspaceEdit;
//然后开启编辑状态:
bool startEdit = workspaceEdit.IsBeingEdited();
if (!startEdit)
{
workspaceEdit.StartEditing(false);
}
workspaceEdit.StartEditOperation();
//字段编辑
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
//定义单个字段
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = TxbFild.Text;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
IClass pClass = pFeatureClass as IClass; //use ITable or IClass
pClass.AddField(pField);
pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
//停止编辑
workspaceEdit.StopEditOperation();
startEdit = workspaceEdit.IsBeingEdited();
if (!startEdit)
{
workspaceEdit.StopEditing(true);
}
//释放对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspace);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFields);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFieldsEdit);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pField);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFieldEdit);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pClass);
BindData();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureClass);
}
else
{ Response.Write("<script>alert('请输入字段名')</script>");
TxbFild.Visible = true;
}
}
/// <summary>
/// 获取要素属性表
/// </summary>
/// <param name="pFeatureClass"></param>
/// <returns></returns>
private static DataTable GetAttributesTable(IFeatureClass pFeatureClass)
{
string geometryType = string.Empty;
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
geometryType = "点";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryMultipoint)
{
geometryType = "点集";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
geometryType = "折线";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
geometryType = "面";
}
// 字段集合
IFields pFields = pFeatureClass.Fields;
int fieldsCount = pFields.FieldCount;
// 写入字段名
DataTable dataTable = new DataTable();
for (int i = 0; i < fieldsCount; i++)
{
dataTable.Columns.Add(pFields.get_Field(i).Name);
}
// 要素游标
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
IFeature pFeature = pFeatureCursor.NextFeature();
if (pFeature == null)
{
return dataTable;
}
else
{
// 获取MZ值
IMAware pMAware = pFeature.Shape as IMAware;
IZAware pZAware = pFeature.Shape as IZAware;
if (pMAware.MAware)
{
geometryType += " M";
}
if (pZAware.ZAware)
{
geometryType += "Z";
}
// 写入字段值
while (pFeature != null)
{
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < fieldsCount; i++)
{
if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
{
dataRow[i] = geometryType;
}
else
{
dataRow[i] = pFeature.get_Value(i).ToString();
}
}
dataTable.Rows.Add(dataRow);
ComReleaser.ReleaseCOMObject(pFeature);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeature);
pFeature = pFeatureCursor.NextFeature();
}
ComReleaser.ReleaseCOMObject(pFeatureCursor); // 释放游标
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureClass);
return dataTable;
}
}
/// <summary>
/// 绑定数据到gridview控件
/// </summary>
private void BindData()
{
IFeatureClass pFeatureClass = GetFeatureClass(@"F:\贵州省石漠化数据服务平台\区县石漠化数据2005to2015\区县石漠化数据2005to2015.shp");
DataTable dataTable = GetAttributesTable(pFeatureClass);
dgvAttributesTable.DataSource = dataTable;
dgvAttributesTable.DataBind();
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureClass);
dgvAttributesTable.Attributes.Add("onclick", "GetData(event)");
}
/// <summary>
/// 获取要素类
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private static IFeatureClass GetFeatureClass(string filePath)
{
AElicense();
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IWorkspaceFactoryLockControl pWorkspaceFactoryLockControl = pWorkspaceFactory as IWorkspaceFactoryLockControl;
if (pWorkspaceFactoryLockControl.SchemaLockingEnabled)//解锁shp文件
{
pWorkspaceFactoryLockControl.DisableSchemaLocking();
}
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0);//System.IO.Path.GetDirectoryName(filePath)作用是获取shp文件所在的文件夹目录
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(System.IO.Path.GetFileName(filePath));
//System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspaceFactory);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspace);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureWorkspace);
return pFeatureClass;
}