DataTableReader.GetSchemaTable 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个描述列元数据的 DataTableReader.DataTable
public:
override System::Data::DataTable ^ GetSchemaTable();
public override System.Data.DataTable GetSchemaTable();
override this.GetSchemaTable : unit -> System.Data.DataTable
Public Overrides Function GetSchemaTable () As DataTable
返回
描述列元数据的 A DataTable 。
例外
示例
以下控制台应用程序示例检索有关指定列的架构信息。 将 DisplaySchemaTableInfo 过程传递一 DataTableReader 个和一个整数,表示列在内 DataTableReader序号位置,过程将架构信息输出到控制台窗口。
private static void TestGetSchemaTable()
{
// Set up the data adapter, using information from
// the AdventureWorks sample database.
// Modify the SQL expression to retrieve
// data from a different table.
SqlDataAdapter adapter =
SetupDataAdapter("SELECT * FROM Sales.Customer");
// Fill the DataTable, retrieving all the schema information.
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataTable table = new DataTable();
adapter.Fill(table);
// Create the DataTableReader, and close it when done.
using (DataTableReader reader = new DataTableReader(table))
{
// Modify the column number to display information
// about a column other than column 0.
DisplaySchemaTableInfo(reader, 0);
}
Console.WriteLine();
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
private static void DisplaySchemaTableInfo(
DataTableReader reader, int ordinal)
{
// Given a DataTableReader, display schema
// information about a particular column.
try
{
DataTable schemaTable = reader.GetSchemaTable();
DataRow row = schemaTable.Rows[ordinal];
foreach (DataColumn col in schemaTable.Columns)
{
Console.WriteLine("{0}: {1}",
col.ColumnName, row[col.Ordinal]);
}
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("{0} is an invalid column number.",
ordinal);
}
}
private static SqlDataAdapter SetupDataAdapter(String sqlString)
{
// Assuming all the default settings, create a
// SqlDataAdapter working with the AdventureWorks
// sample database that's available with
// SQL Server.
String connectionString =
"Data source=(local);initial catalog=AdventureWorks;" +
"Integrated Security=True";
return new SqlDataAdapter(sqlString, connectionString);
}
Private Sub TestGetSchemaTable()
' Set up the data adapter, using information from
' the AdventureWorks sample database.
' Modify the SQL expression to retrieve
' data from a different table.
Dim adapter As SqlDataAdapter = _
SetupDataAdapter("SELECT * FROM Sales.Customer")
' Fill the DataTable, retrieving all the schema information.
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim table As New DataTable
adapter.Fill(table)
' Create the DataTableReader, and close it when done.
Using reader As New DataTableReader(table)
' Modify the column number to display information
' about a column other than column 0.
DisplaySchemaTableInfo(reader, 0)
End Using
Console.WriteLine()
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Sub
Private Sub DisplaySchemaTableInfo( _
ByVal reader As DataTableReader, ByVal ordinal As Integer)
' Given a DataTableReader, display schema
' information about a particular column.
Try
Dim schemaTable As DataTable = reader.GetSchemaTable()
Dim row As DataRow = schemaTable.Rows(ordinal)
For Each col As DataColumn In schemaTable.Columns
Console.WriteLine("{0}: {1}", _
col.ColumnName, row(col.Ordinal))
Next
Catch ex As IndexOutOfRangeException
Console.WriteLine("{0} is an invalid column number.", _
ordinal)
End Try
End Sub
Private Function SetupDataAdapter( _
ByVal sqlString As String) As SqlDataAdapter
' Assuming all the default settings, create a SqlDataAdapter
' working with the AdventureWorks sample database that's
' available with SQL Server.
Dim connectionString As String = _
"Data Source=(local);" & _
"Initial Catalog=AdventureWorks;" & _
"Integrated Security=true"
Return New SqlDataAdapter(sqlString, connectionString)
End Function
注解
GetSchemaTable 方法按以下顺序返回有关每个列的元数据:
| DataReader 列 | 说明 |
|---|---|
| ColumnName | 列在 .. 中 DataTable显示时的名称。 |
| ColumnOrdinal | 列的序号 |
| 列大小 | 如果 ColumnSize 无法确定或不相关的 (或 MaxLength) 属性 DataColumn ,则为 -1;否则为 0 或包含 MaxLength 值的正整数。 |
| NumericPrecision | 如果列类型为数值类型,则这是列的最大精度。 如果列类型不是数值数据类型,则为 null 值。 |
| NumericScale | 如果列数据类型具有缩放组件,则返回小数点右侧的数字数。 否则,返回 null 值。 |
| 数据类型 | 列的基础类型。 |
| ProviderType | 列数据类型的指示器。 如果列的数据类型因行而异,则此值为 Object。 此列不能包含 null 值。 |
| IsLong |
true 如果列的数据类型为 String ,并且其属性为 -1,则为 MaxLength -1。 否则为 false。 |
| AllowDBNull |
true 如果 AllowDbNull 约束设置为列,则为 true;否则,为 false. |
| IsReadOnly |
true 如果无法修改列,则为否则 false。 |
| IsRowVersion |
false,对于每个列。 |
| IsUnique |
true:此列中的两行 DataTable 不能具有相同的值。
IsUnique 如果列本身表示键,或者存在仅适用于此列的 UNIQUE 类型的约束,则保证为 true。
false:列可以包含值中的 DataTable重复值。 此列的默认值为 false. |
| IsKey |
true:列是一组列之一,这些列组合在一起,唯一标识其中的 DataTable行。 设置为IsKeytrue唯一标识行的DataTable列集。 不需要此列集是最小列集。 可以从主键、唯一 DataTable 约束或唯一索引生成这组列。
false:列不需要唯一标识行。 如果列参与单个或复合主键,则此值 true 为 。 否则,其值 false。 |
| IsAutoIncrement |
true:列以固定增量将值分配给新行。
false:该列不会以固定增量为新行赋值。 此列的默认值为 false. |
| BaseCatalogName | 包含列的数据存储区中的目录的名称。
Null 如果无法确定基目录名称,则为 。 此列的默认值为一个 null 值。 |
| BaseSchemaName | 此值始终 Null为 . |
| BaseTableName | DataTable的名称。 |
| BaseColumnName | 中 DataTable列的名称。 |
| AutoIncrementSeed | 属性AutoIncrementSeed的值DataTable。 |
| AutoIncrementStep | 属性AutoIncrementStep的值DataTable。 |
| 默认值 | 属性DefaultValue的值DataColumn。 |
| 表达式 | 如果当前列是表达式列,并且表达式中使用的所有列都属于包含表达式列的列 T:System.Data.DataTable ,则表达式字符串;否则 null为 。 |
| ColumnMapping | 与 MappingType . DataColumn. 关联的值 类型可以是其中AttributeElementHidden一种,也可以。SimpleContent 默认值为 Element。 |
| BaseTableNamespace | 属性Namespace的值DataTable。 |
| BaseColumnNamespace | 属性Namespace的值DataColumn。 |