public static ITable ToITable(DataTable mTable) { try { #region 新建表字段 IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; for (int i = 0; i < mTable.Columns.Count; i++) { IField pField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)pField; fieldEdit.Name_2 = mTable.Columns[i].ColumnName; fieldEdit.AliasName_2 = mTable.Columns[i].ColumnName; //这里根据实际情况设置 if (i == 0) { fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; } else { fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; } fieldEdit.Editable_2 = true; fieldsEdit.AddField(pField); } #endregion // 实例化内存工作空间工厂 IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); // 创建内存工作空间 IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); IName name = (IName)workspaceName; // 通过name对象打开工作空间 IWorkspace workspace = (IWorkspace)name.Open(); IFeatureWorkspace pFWS = workspace as IFeatureWorkspace; //创建空表 ITable pTable = pFWS.CreateTable("tempTable", fieldsEdit, null, null, ""); //获取DataTble中记录的数 int count = mTable.Rows.Count; //转换为ITable中的数据 for (int i = 0; i < count; i++) { //在ITable中创建新纪录;获取DataTble中的记录 IRow row = pTable.CreateRow(); DataRow pRrow = mTable.Rows[i]; //列总数 int rowNum = pRrow.ItemArray.Length; // 添加记录 for ( int j = 1; j < rowNum ; j++) { row.set_Value(j, pRrow.ItemArray.GetValue(j)); row.Store(); } } return pTable; } catch (Exception ex) { return null; } }
补充:在开发中发现,内存中的ITable无法作为参数传递给“连接字段”GP工具,会报错:传递的参数不是表。如果用IGPUtilities接口的MakeGPTableView方法生成IGPTableView再传递给“连接字段”GP工具时,会报错:Error 000732:数据集×××不存在或不被支持。