public static EsriFeatureSet AsEsriFeatureSet(this SqlFeatureSet featureSet) { var result = new EsriFeatureSet(); result.Fields = featureSet.Fields.ToArray(); result.Features = featureSet.Features.Select(f => new EsriFeature() { Attributes = f.Attributes.ToDictionary(i => i.Key, i => i.Value == null ? string.Empty : i.Value.ToString()), Geometry = f.TheSqlGeometry.AsEsriJsonGeometry() }).ToArray(); return(result); }
private SqlFeatureSet QueryFeatures(string selectQuery) { SqlConnection connection = new SqlConnection(_connectionString); SqlFeatureSet result = new SqlFeatureSet(this.GetSrid()) { Fields = new List <Field>(), Features = new List <SqlFeature>() }; try { var command = new SqlCommand(selectQuery, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); for (int i = 0; i < reader.FieldCount; i++) { var type = reader.GetFieldType(i); if (type != typeof(SqlGeometry)) { result.Fields.Add(new Field() { Name = reader.GetName(i), Type = type.ToString() }); } } if (!reader.HasRows) { return(result); } while (reader.Read()) { var dict = new Dictionary <string, object>(); var feature = new SqlFeature(); for (int i = 0; i < reader.FieldCount; i++) { var fieldName = reader.GetName(i); while (dict.Keys.Contains(fieldName)) { fieldName = $"{fieldName}_"; } if (reader.IsDBNull(i)) { dict.Add(fieldName, null); } else { if (reader[i] is SqlGeometry) { feature.TheSqlGeometry = (SqlGeometry)reader[i]; } else { dict.Add(fieldName, reader[i]); } } } if (!string.IsNullOrWhiteSpace(IdColumnName)) { feature.Id = (int)dict[IdColumnName]; } feature.Attributes = dict; result.Features.Add(feature); } connection.Close(); } catch (Exception ex) { connection.Close(); } return(result); }