private static QueryResultInfo GetQueryResultInfo_NetStandard(this IDataReader reader) { #if NETSTANDARD2_0 var colinfo = reader as IDbColumnSchemaGenerator; if (colinfo != null) { var res = new QueryResultInfo(); foreach (var row in colinfo.GetColumnSchema()) { var col = new QueryResultColumnInfo(); int size = row.ColumnSize ?? 0; col.Name = row.ColumnName; col.NotNull = !(row.AllowDBNull ?? true); col.DataType = row.DataTypeName; col.Size = size; col.CommonType = ReaderDataType(row); col.BaseColumnName = row.BaseColumnName; col.BaseSchemaName = row.BaseSchemaName; col.BaseTableName = row.BaseTableName; col.BaseServerName = row.BaseServerName; col.BaseCatalogName = row.BaseCatalogName; if (row.IsAutoIncrement ?? false) { col.CommonType.SetAutoincrement(true); col.AutoIncrement = true; } if (row.IsKey ?? false) { col.IsKey = true; } if (row.IsHidden ?? false) { col.IsHidden = true; } if (row.IsReadOnly ?? false) { col.IsReadOnly = true; } if (row.IsAliased ?? false) { col.IsAliased = true; } res.Columns.Add(col); } return(res); } return(null); #else retrun null; #endif }
private static QueryResultInfo GetQueryResultInfo_OnlyColumnNames(this IDataReader reader) { try { var res = new QueryResultInfo(); for (int i = 0; i < reader.FieldCount; i++) { var column = new QueryResultColumnInfo(); column.Name = reader.GetName(i); res.Columns.Add(column); } return(res); } catch { return(null); } }
public static QueryResultInfo SchemaTableToInfo(DataTable schemaTable) { var res = new QueryResultInfo(); foreach (DataRow row in schemaTable.Rows.SortedByKey <DataRow, int>(row => Int32.Parse(row["ColumnOrdinal"].ToString()))) { var col = new QueryResultColumnInfo(); int size = row.SafeString("ColumnSize").SafeIntParse(); col.Name = row.SafeString("ColumnName"); if (row["AllowDBNull"] is bool notNull) { col.NotNull = !notNull; } int dataTypeNameIndex = row.Table.Columns.GetOrdinal("DataTypeName"); int dataTypeIndex = row.Table.Columns.GetOrdinal("DataType"); if (dataTypeNameIndex >= 0) { col.DataType = row[dataTypeNameIndex].SafeToString(); } else if (dataTypeIndex >= 0) { col.DataType = (row[dataTypeIndex] as Type)?.Name ?? "String"; } else { col.DataType = "String"; } col.Size = size; col.CommonType = ReaderDataType(row); col.BaseColumnName = row.SafeString("BaseColumnName"); col.BaseSchemaName = row.SafeString("BaseSchemaName"); col.BaseTableName = row.SafeString("BaseTableName"); col.BaseServerName = row.SafeString("BaseServerName"); col.BaseCatalogName = row.SafeString("BaseCatalogName"); if (row.SafeBool("IsAutoIncrement", false)) { col.CommonType.SetAutoincrement(true); col.AutoIncrement = true; } if (row.SafeBool("IsKey", false)) { col.IsKey = true; } if (row.SafeBool("IsHidden", false)) { col.IsHidden = true; } if (row.SafeBool("IsReadOnly", false)) { col.IsReadOnly = true; } if (row.SafeBool("IsAliased", false)) { col.IsAliased = true; } res.Columns.Add(col); } return(res); }