Private Function ConvDT_To_ITab(ByVal inDataTable As DataTable, ByVal inITableName As String) As ITable Try '**Open the PGDB for temp layers Dim db_path As String = "C:\yourpathname\yourPGDBname.mdb" Dim pWS As IWorkspace Dim pFWS As IFeatureWorkspace Dim pWorkspaceFactory As IWorkspaceFactory pWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory pFWS = pWorkspaceFactory.OpenFromFile(db_path, 0) pWS = pFWS ' Set up a simple fields collection Dim pFields As ESRI.ArcGIS.Geodatabase.IFields Dim pFieldsEdit As ESRI.ArcGIS.Geodatabase.IFieldsEdit pFields = New ESRI.ArcGIS.Geodatabase.Fields pFieldsEdit = New ESRI.ArcGIS.Geodatabase.Fields pFieldsEdit = pFields Dim pField As ESRI.ArcGIS.Geodatabase.IField Dim pFieldEdit As ESRI.ArcGIS.Geodatabase.IFieldEdit pField = New ESRI.ArcGIS.Geodatabase.Field pFieldEdit = pField '' create the object id field pField = New ESRI.ArcGIS.Geodatabase.Field pFieldEdit = pField pFieldEdit.Name_2 = "OBJECTID" pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID pFieldsEdit.AddField(pField) For i = 0 To inDataTable.Columns.Count - 1 pField = New ESRI.ArcGIS.Geodatabase.Field pFieldEdit = pField With pFieldEdit 'set all the properties according to the columns in the recordset .Length_2 = inDataTable.Columns(i).MaxLength .Name_2 = inDataTable.Columns(i).ColumnName .Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger End With Dim ColName As String = inDataTable.Columns(i).ColumnName.ToString Dim dtColType As String = inDataTable.Columns(i).DataType.ToString If inDataTable.Columns(i).DataType.FullName.ToString = "System.Int32" Then pFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger ElseIf inDataTable.Columns(i).DataType.FullName.ToString = "System.String" Then pFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString ElseIf inDataTable.Columns(i).DataType.FullName.ToString = "System.Double" Then pFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDouble ElseIf inDataTable.Columns(i).DataType.FullName.ToString = "System.Decimal" Then pFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDouble ElseIf inDataTable.Columns(i).DataType.FullName.ToString = "System.DateTime" Then pFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDate End If pFieldsEdit.AddField(pField) Next i '**Check for existing FeatClass and delete if present Dim pExistingDs As ESRI.ArcGIS.Geodatabase.IDataset Dim pEnumDataset As IEnumDataset pEnumDataset = pWS.Datasets(esriDatasetType.esriDTTable) pExistingDs = pEnumDataset.Next Do Until pExistingDs Is Nothing If pExistingDs.Name = inITableName Then If pExistingDs.CanDelete Then pExistingDs.Delete() End If End If pExistingDs = pEnumDataset.Next Loop Dim UID_FEATURE As String = "esriGeodatabase.Object" '** create a UID for simple features Dim pUID As ESRI.ArcGIS.esriSystem.UID pUID = New ESRI.ArcGIS.esriSystem.UID pUID.Value = UID_FEATURE ''** Create new FeatClass in PGDB Dim newITable As ITable = pFWS.CreateTable(inITableName, _ pFields, _ pUID, _ Nothing, Nothing) Dim pCur As ICursor Dim pRowBuf As IRowBuffer pRowBuf = newITable.CreateRowBuffer pCur = newITable.Insert(True) Dim dtCol As DataColumn Dim dtColname As String Dim row As DataRow For i = 0 To inDataTable.Rows.Count - 1 row = inDataTable.Rows(i) If Not IsDBNull(row.Item(0)) Then '***add the rest of the attributes For Each dtCol In inDataTable.Columns dtColname = dtCol.ColumnName.ToString Dim dtColType As String = dtCol.DataType.ToString If Not IsDBNull(row.Item(dtColname)) Then pRowBuf.Value(pRowBuf.Fields.FindField(dtColname)) = row.Item(dtColname) Else If dtCol.DataType.FullName.ToString = "System.Int32" Then pRowBuf.Value(pRowBuf.Fields.FindField(dtColname)) = CInt(0) ElseIf dtCol.DataType.FullName.ToString = "System.String" Then pRowBuf.Value(pRowBuf.Fields.FindField(dtColname)) = CStr("NA") ElseIf dtCol.DataType.FullName.ToString = "System.Double" Then pRowBuf.Value(pRowBuf.Fields.FindField(dtColname)) = CDbl(0.0) ElseIf dtCol.DataType.FullName.ToString = "System.Decimal" Then pRowBuf.Value(pRowBuf.Fields.FindField(dtColname)) = CDec(0.0) ElseIf dtCol.DataType.FullName.ToString = "System.DateTime" Then pRowBuf.Value(pRowBuf.Fields.FindField(dtColname)) = CDate(Now()) End If End If Next pCur.InsertRow(pRowBuf) Else End If Next pCur.Flush() Return newITable Catch ex As Exception MsgBox(ex.ToString) Return Nothing End Try End Function
I don't want to convert the ADO-Datatable to an ESRI-Table, I thought there is a possiblity to use the Datatable directly thru QueryInterface.
Hello James,
thank you for your answers. Now I have a program, that works:
I make a ADO-Datatable with a SQL-Query.
Then I convert the ADO-Datatable into ITable and from this a made a XY-Event-Layer.
To update the Layer I refill the ADO-Datatable, then I delete all Rows in the ITable and transfer the ADO-Datatable in this ITable again, so the reference between XYEventLayer and ITable is the same.
It requires at this moment, that the structure has not been changed. Also I only tested with 30 points. But I'm confident that the performance is acceptable.
Again, many thanks for all. 🙂
Thanks for the code jamesfreddyc... best example I've found.