/// <summary> /// Creates a DataTable representing the information in a shape file. /// </summary> /// <param name="filename">The filename (minus the . and extension) to read.</param> /// <param name="tableName">The name to give to the table.</param> /// <param name="geometryFactory">The geometry factory to use when creating the objects.</param> /// <returns>DataTable representing the data </returns> public static DataTable CreateDataTable(string filename, string tableName, GeometryFactory geometryFactory) { if (filename == null) { throw new ArgumentNullException("filename"); } if (tableName == null) { throw new ArgumentNullException("tableName"); } if (geometryFactory == null) { throw new ArgumentNullException("geometryFactory"); } ShapefileDataReader shpfileDataReader = new ShapefileDataReader(filename, geometryFactory); DataTable table = new DataTable(tableName); // use ICustomTypeDescriptor to get the properies/ fields. This way we can get the // length of the dbase char fields. Because the dbase char field is translated into a string // property, we lost the length of the field. We need to know the length of the // field when creating the table in the database. IEnumerator enumerator = shpfileDataReader.GetEnumerator(); bool moreRecords = enumerator.MoveNext(); ICustomTypeDescriptor typeDescriptor = (ICustomTypeDescriptor)enumerator.Current; foreach (PropertyDescriptor property in typeDescriptor.GetProperties()) { ColumnStructure column = (ColumnStructure)property; Type fieldType = column.PropertyType; DataColumn datacolumn = new DataColumn(column.Name, fieldType); if (fieldType == typeof(string)) { // use MaxLength to pass the length of the field in the dbase file datacolumn.MaxLength = column.Length; } table.Columns.Add(datacolumn); } // add the rows - need a do-while loop because we read one row in order to determine the fields int iRecordCount = 0; object[] values = new object[shpfileDataReader.FieldCount]; do { iRecordCount++; shpfileDataReader.GetValues(values); table.Rows.Add(values); moreRecords = enumerator.MoveNext(); }while (moreRecords); return(table); }