public class CustomMsSql2008FeatureLayer : MsSql2008FeatureLayer { public CustomMsSql2008FeatureLayer(string connectionString, string tableName, string featureIdColumn) : base() { FeatureSource = new CustomMsSql2008FeatureSource(connectionString, tableName, featureIdColumn); } } public class CustomMsSql2008FeatureSource : MsSql2008FeatureSource { public CustomMsSql2008FeatureSource(string connectionString, string tableName, string featureIdColumn) { this.ConnectionString = connectionString; this.TableName = tableName; this.FeatureIdColumn = featureIdColumn; this.CommandTimeout = 30; this.Srid = 4326; this.WhereClause = string.Empty; } public string WkbFieldName { get; set; } protected override Collection GetColumnsCore() { string sqlStatement = "SELECT allColumns.name, allTypes.name, allTypes.max_length FROM sys.columns allColumns, sys.all_views allViews, sys.types allTypes where allColumns.object_id=allViews.object_id AND allColumns.user_type_id=allTypes.user_type_id AND allViews.name='" + TableName + "';"; SqlCommand command = null; SqlDataReader dataReader = null; Collection columnCollection = new Collection(); try { command = GetSqlCommand(sqlStatement, ExecutingSqlStatementType.GetColumns); dataReader = command.ExecuteReader(); if (dataReader.HasRows) { while (dataReader.Read()) { string columnName = dataReader[0].ToString(); string typeName = dataReader[1].ToString(); int maxLength = Convert.ToInt32(dataReader[2], CultureInfo.InvariantCulture); FeatureSourceColumn column = new FeatureSourceColumn(columnName, typeName, maxLength); columnCollection.Add(column); } } else { if (dataReader != null) { dataReader.Dispose(); } command.CommandText = string.Format(CultureInfo.InvariantCulture, "select * from {0} where 1=0;", TableName); DataTable dataTable = new DataTable(); dataTable.Locale = CultureInfo.InvariantCulture; SqlDataAdapter adpter = new SqlDataAdapter(command); adpter.Fill(dataTable); foreach (DataColumn column in dataTable.Columns) { string columnName = column.ColumnName; string typeName = column.DataType.Name; int maxLength = column.MaxLength; if (columnName == WkbFieldName) { typeName = "geometry"; } FeatureSourceColumn featureColumn = new FeatureSourceColumn(columnName, typeName, maxLength); columnCollection.Add(featureColumn); } adpter.Dispose(); dataTable.Dispose(); } } finally { if (command != null) { command.Connection.Close(); command.Dispose(); } if (dataReader != null) { dataReader.Dispose(); } } return columnCollection; } private SqlCommand GetSqlCommand(string sqlStatement, ExecutingSqlStatementType sqlStatementType) { if (sqlStatementType != ExecutingSqlStatementType.GetSpatialDataType && sqlStatementType != ExecutingSqlStatementType.GetColumns && sqlStatementType != ExecutingSqlStatementType.BuildIndex && sqlStatementType != ExecutingSqlStatementType.GetBoundingBox) { if (!string.IsNullOrEmpty(WhereClause)) { sqlStatement = sqlStatement.Replace(';', ' '); if (sqlStatement.ToUpper(CultureInfo.InvariantCulture).Contains("WHERE")) { sqlStatement = sqlStatement + " " + WhereClause.ToUpper(CultureInfo.InvariantCulture).Replace("WHERE", "AND"); } else { sqlStatement = sqlStatement + " " + WhereClause; } sqlStatement += ";"; } } ExecutingSqlStatementMsSql2008FeatureSourceEventArgs e = new ExecutingSqlStatementMsSql2008FeatureSourceEventArgs(sqlStatement, sqlStatementType); OnExecutingSqlStatement(e); SqlConnection connection = new SqlConnection(ConnectionString); connection.Open(); SqlCommand command = new SqlCommand(e.SqlStatement, connection); command.CommandTimeout = CommandTimeout; return command; } }