【转】ArcGIS Engine ITable 与System.DataTable相互转换
来源:互联网 发布:兼职 大数据 英文 翻译 编辑:程序博客网 时间:2024/04/30 12:46
源码不知道出自哪了!没有验证过,但应该可以用
/// <summary>
/// 打开dbf表
/// </summary>
/// <param name="pathName"></param>
/// <param name="tableName"></param>
/// <returns></returns>
public staticITable OpenTable(stringpathName,string tableName)
{
// Create the workspace name object.
IWorkspaceName workspaceName = new WorkspaceNameClass();
workspaceName.PathName= pathName;
workspaceName.WorkspaceFactoryProgID= "esriDataSourcesFile.shapefileworkspacefactory";
// Create the table name object.
IDatasetName dataSetName = new TableNameClass();
dataSetName.Name= tableName;
dataSetName.WorkspaceName= workspaceName;
// Open the table.
IName name =(IName)dataSetName;
ITable table = (ITable)name.Open();
return table;
}
/// <summary>
/// 将ITable转换为DataTable
/// </summary>
/// <param name="mTable"></param>
/// <returns></returns>
public staticDataTable ToDataTable(ITablemTable)
{
try
{
DataTablepTable = new DataTable();
for (int i = 0; i < mTable.Fields.FieldCount; i++)
{
pTable.Columns.Add(mTable.Fields.get_Field(i).Name);
}
ICursorpCursor = mTable.Search(null,false);
IRow pRrow= pCursor.NextRow();
while (pRrow!= null)
{
DataRowpRow = pTable.NewRow();
string[]StrRow = new string[pRrow.Fields.FieldCount];
for (int i = 0; i < pRrow.Fields.FieldCount; i++)
{
StrRow[i] = pRrow.get_Value(i).ToString();
}
pRow.ItemArray = StrRow;
pTable.Rows.Add(pRow);
pRrow = pCursor.NextRow();
}
returnpTable;
}
catch (Exceptionex)
{
return null;
}
}
/// <summary>
/// 把DataTable转为ITable,tempPath不含文件名的问价夹路径
/// </summary>
/// <param name="mTable"></param>
/// <returns></returns>
public staticITable ToITable(DataTablemTable,string tempPath)
{
try
{
#region 新建表字段
IFieldpField = null;
IFieldsfields = new FieldsClass();
IFieldsEditfieldsEdit = (IFieldsEdit)fields;
fieldsEdit.FieldCount_2 = 3;
pField = new FieldClass();
IFieldEditfieldEdit = (IFieldEdit)pField;
fieldEdit.Name_2 = "FromField";
fieldEdit.AliasName_2 = "开始字段值";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldEdit.Editable_2 = true;
//添加开始字段
fieldsEdit.set_Field(0, pField);
IFieldpField1 = new FieldClass();
IFieldEditfieldEdit1 = (IFieldEdit)pField1;
fieldEdit1.Name_2 = "ToField";
fieldEdit1.AliasName_2 = "结束字段值";
fieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldEdit1.Editable_2 = true;
//添加结束字段
fieldsEdit.set_Field(1, pField1);
IFieldpField2 = new FieldClass();
IFieldEditfieldEdit2 = (IFieldEdit)pField2;
fieldEdit2.Name_2 = "outField";
fieldEdit2.AliasName_2 = "分类字段值";
fieldEdit2.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldEdit2.Editable_2 = true;
//添加重分类字段
fieldsEdit.set_Field(2, pField2);
#endregion
ShapefileWorkspaceFactoryClass class2 = new ShapefileWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace =class2.OpenFromFile(tempPath, 0);
IFeatureWorkspacepFWS = pWorkspaceas IFeatureWorkspace;
//删除已有的
if(System.IO.File.Exists(tempPath +"重分类.dbf"))
{
System.IO.File.Delete(tempPath+"重分类.dbf");
}
//创建空表
ESRI.ArcGIS.Geodatabase.ITable pTable;
pTable = pFWS.CreateTable("重分类", fieldsEdit,null, null, "");
//获取表中记录数
int count =mTable.Rows.Count;
//转换为ITable中的数据
for (int k = 0; k < count; k++)
{
//ITable 的记录
IRow row= pTable.CreateRow();
DataRowpRrow = mTable.Rows[k];
//列元素
int rowNum =pRrow.ItemArray.Length;
// 添加记录
for (int n = 1; n < rowNum + 1; n++)
{
row.set_Value(n, pRrow.ItemArray.GetValue(n- 1));
row.Store();
}
}
returnpTable;
}
catch (Exceptionex)
{
return null;
}
}
/// <summary>
///保存DataTable表位DBF ,tempPath文件完整路径
/// </summary>
/// <param name="mTable"></param>
/// <returns></returns>
public staticbool SaveTable(DataTablemTable,string tempPath)
{
try
{
#region 新建表字段
IFieldpField = null;
IFieldsfields = new FieldsClass();
IFieldsEditfieldsEdit = (IFieldsEdit)fields;
fieldsEdit.FieldCount_2 = 3;
pField = new FieldClass();
IFieldEditfieldEdit = (IFieldEdit)pField;
fieldEdit.Name_2 = "FromField";
fieldEdit.AliasName_2 = "开始字段值";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldEdit.Editable_2 = true;
//添加开始字段
fieldsEdit.set_Field(0, pField);
IFieldpField1 = new FieldClass();
IFieldEditfieldEdit1 = (IFieldEdit)pField1;
fieldEdit1.Name_2 = "ToField";
fieldEdit1.AliasName_2 = "结束字段值";
fieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldEdit1.Editable_2 = true;
//添加结束字段
fieldsEdit.set_Field(1, pField1);
IFieldpField2 = new FieldClass();
IFieldEditfieldEdit2 = (IFieldEdit)pField2;
fieldEdit2.Name_2 = "outField";
fieldEdit2.AliasName_2 = "分类字段值";
fieldEdit2.Type_2 = esriFieldType.esriFieldTypeDouble;
fieldEdit2.Editable_2 = true;
//添加重分类字段
fieldsEdit.set_Field(2, pField2);
#endregion
string path =System.IO.Path.GetDirectoryName(tempPath);
stringfileName = System.IO.Path.GetFileName(tempPath);
ShapefileWorkspaceFactoryClass class2 = new ShapefileWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = class2.OpenFromFile(path,0);
IFeatureWorkspacepFWS = pWorkspaceas IFeatureWorkspace;
//删除已有的
if(System.IO.File.Exists(tempPath))
{
System.IO.File.Delete(tempPath);
}
fileName = fileName.Split('.')[0];
//创建空表
ESRI.ArcGIS.Geodatabase.ITable pTable;
pTable = pFWS.CreateTable(fileName,fieldsEdit, null,null,"");
//获取表中记录数
int count =mTable.Rows.Count;
//转换为ITable中的数据
for (int k = 0; k < count; k++)
{
//ITable 的记录
IRow row= pTable.CreateRow();
DataRowpRrow = mTable.Rows[k];
//列元素
int rowNum =pRrow.ItemArray.Length;
// 添加记录
for (int n = 1; n < rowNum + 1; n++)
{
row.set_Value(n, pRrow.ItemArray.GetValue(n- 1));
row.Store();
}
}
return true;
}
catch (Exceptionex)
{
return false;
}
}
- 【转】ArcGIS Engine ITable 与System.DataTable相互转换
- 【转载】ArcEngine ITable 与System.DataTable相互转换
- json与DataTable相互转换
- DataTable与Linq相互转换
- DataTable与Linq相互转换
- DataTable与数组相互转换
- ArcGIS Engine中的刷新与不同坐标间的相互转换
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- DataTable与Linq相互转换方法
- Xml与DataTable相互转换方法
- Xml与DataTable相互转换方法
- DataTable与Linq相互转换方法
- json与datatable的相互转换
- DataTable与Linq相互转换方法
- DataTable 与 实体之间相互转换
- Basic view
- 陌陌,该甩掉你“约炮”的帽子了
- 不是职业规划的职业规划
- 简述java中的final、finally、finalize的差别
- struts2 action 文件下载的配置
- 【转】ArcGIS Engine ITable 与System.DataTable相互转换
- MsiQueryProductState
- UVA 10254 - The Priest Mathematician(规律)
- 2013年美国开发者薪资调查报告
- python 字符串前加r
- [HGE]-源码分析-14 system (c)
- Hibernate实现分页和综合查询详解
- C#客户端 C++服务端 乱码问题
- 五大常用算法之一:分治算法(转载)