DbDataAdapter.FillSchema 方法

定义

DataTable 添加到 DataSet 中,并配置架构以匹配数据源中的架构。Adds a DataTable to a DataSet and configures the schema to match that in the data source.

重载

FillSchema(DataSet, SchemaType, IDbCommand, String, CommandBehavior)

DataTable 添加到指定的 DataSet 中,并根据指定的 SchemaType 配置架构以匹配数据源中的架构。Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType.

FillSchema(DataTable, SchemaType, IDbCommand, CommandBehavior)

根据指定的 DataTable、命令字符串以及 SchemaType 值配置指定 CommandBehavior 的架构。Configures the schema of the specified DataTable based on the specified SchemaType, command string, and CommandBehavior values.

FillSchema(DataSet, SchemaType, String)

DataTable 添加到指定的 DataSet 中,并根据指定的 SchemaTypeDataTable 配置架构以匹配数据源中的架构。Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based upon the specified SchemaType and DataTable.

FillSchema(DataTable, SchemaType)

根据指定的 DataTable 配置指定 SchemaType 的架构。Configures the schema of the specified DataTable based on the specified SchemaType.

FillSchema(DataSet, SchemaType)

将名为“Table”的 DataTable 添加到指定的 DataSet,并根据指定的 SchemaType 配置架构以匹配数据源中的架构。Adds a DataTable named "Table" to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType.

FillSchema(DataSet, SchemaType, IDbCommand, String, CommandBehavior)

DataTable 添加到指定的 DataSet 中,并根据指定的 SchemaType 配置架构以匹配数据源中的架构。Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType.

protected:
 virtual cli::array <System::Data::DataTable ^> ^ FillSchema(System::Data::DataSet ^ dataSet, System::Data::SchemaType schemaType, System::Data::IDbCommand ^ command, System::String ^ srcTable, System::Data::CommandBehavior behavior);
protected virtual System.Data.DataTable[] FillSchema (System.Data.DataSet dataSet, System.Data.SchemaType schemaType, System.Data.IDbCommand command, string srcTable, System.Data.CommandBehavior behavior);
override this.FillSchema : System.Data.DataSet * System.Data.SchemaType * System.Data.IDbCommand * string * System.Data.CommandBehavior -> System.Data.DataTable[]

参数

dataSet
DataSet

要用数据源中的架构填充的 DataSetThe DataSet to be filled with the schema from the data source.

schemaType
SchemaType

SchemaType 值之一。One of the SchemaType values.

command
IDbCommand

用于从数据源中检索行的 SQL SELECT 语句。The SQL SELECT statement used to retrieve rows from the data source.

srcTable
String

用于表映射的源表的名称。The name of the source table to use for table mapping.

behavior
CommandBehavior

CommandBehavior 值之一。One of the CommandBehavior values.

返回

DataTable 对象的数组,这些对象包含从数据源返回的架构信息。An array of DataTable objects that contain schema information returned from the data source.

注解

FillSchema 方法使用 SelectCommand从数据源检索架构。The FillSchema method retrieves the schema from the data source using the SelectCommand. SelectCommand 关联的连接对象必须有效,但不需要打开。The connection object associated with the SelectCommand must be valid, but it does not need to be open. 如果在调用 FillSchema 之前关闭连接,则会打开该连接,以检索数据,然后关闭。If the connection is closed before FillSchema is called, it is opened to retrieve data, then closed. 如果在调用 FillSchema 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before FillSchema is called, it remains open.

FillSchema 操作向目标 DataSet添加 DataTableA FillSchema operation adds a DataTable to the destination DataSet. 然后,将列添加到 DataTableDataColumnCollection 中,并配置以下 DataColumn 属性(如果它们存在于数据源中):It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source:

FillSchema 还根据以下规则配置 PrimaryKeyConstraints 属性:FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:

  • 如果 SelectCommand返回一个或多个主键列,则将它们用作 DataTable的主键列。If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable.

  • 如果未返回主键列但唯一列为,则当且仅当不可 null 所有唯一列时,才将唯一列用作主键。If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. 如果任何列可为 null,则会将 UniqueConstraint 添加到 ConstraintCollection,但不会设置 PrimaryKey 属性。If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set.

  • 如果为主键列和唯一列都返回,则主键列将用作 DataTable的主键列。If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable.

请注意,根据前面的规则将主键和 unique 约束添加到 ConstraintCollection 中,但不会添加其他约束类型。Note that primary keys and unique constraints are added to the ConstraintCollection according to the preceding rules, but other constraint types are not added.

如果在 SQL Server 表中的一列或多列上定义了唯一聚集索引,并且在一组单独的列上定义了 primary key 约束,则将返回聚集索引中的列的名称。If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. 若要返回主键列的名称,请使用带有指定主键索引名称的 SELECT 语句的查询提示。To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. 有关指定查询提示的详细信息,请参阅提示(transact-sql)-查询For more information about specifying query hints, see Hints (Transact-SQL) - Query.

如果 IDataAdapter 在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等来为后续列生成名称。If the IDataAdapter encounters duplicate columns while populating a DataTable, it generates names for the subsequent columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. 如果传入数据包含未命名列,则这些列将按照模式 "Column1"、"Column2" 等放置在 DataSet 中。If the incoming data contains unnamed columns, they are placed in the DataSet according to the pattern "Column1", "Column2", and so on. 当将多个结果集添加到 DataSet 时,每个结果集将放在一个单独的表中。When multiple result sets are added to the DataSet each result set is placed in a separate table. 附加的结果集通过将整数值追加到指定的表名来命名(例如,"表"、"Table1"、"Table2" 等)。Additional result sets are named by appending integral values to the specified table name (for example, "Table", "Table1", "Table2", and so on.). 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

FillSchema 方法支持这样的方案: DataSet 包含多个 DataTable 对象,这些对象的名称仅区分大小写。The FillSchema method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 在这种情况下,FillSchema 执行区分大小写的比较来查找相应的表,如果不存在完全匹配项,则创建新表。In such situations, FillSchema performs a case-sensitive comparison to find the corresponding table, and creates a new table if no exact match exists. 下面C#的代码演示了此行为。The following C# code illustrates this behavior.

DataSet dataset = new DataSet();  
dataset.Tables.Add("aaa");  
dataset.Tables.Add("AAA");  
adapter.FillSchema(dataset, "aaa"); // Fills the schema of "aaa", which already exists in the DataSet.  
adapter.FillSchema(dataset, "Aaa"); // Adds a new table called "Aaa".  

如果调用 FillSchemaDataSet 只包含一个名称只是大小写不同的 DataTable,则将更新该 DataTableIf FillSchema is called and the DataSet contains only one DataTable whose name differs only by case, that DataTable is updated. 在这种情况下,比较不区分大小写。In this scenario, the comparison is case insensitive. 下面C#的代码演示了此行为。The following C# code illustrates this behavior.

DataSet dataset = new DataSet();  
dataset.Tables.Add("aaa");  
adapter.FillSchema(dataset, "AAA"); // Fills the schema of table "aaa" because only one similarly named table is in the DataSet.  

FillSchema 不返回任何行。FillSchema does not return any rows. 使用 Fill 方法将行添加到 DataTable中。Use the Fill method to add rows to a DataTable.

备注

当处理返回多个结果的批处理 SQL 语句时,OLE DB 的 .NET Framework 数据提供程序 FillSchema 的实现仅检索第一个结果的架构信息。When handling batch SQL statements that return multiple results, the implementation of FillSchema for the .NET Framework Data Provider for OLE DB retrieves schema information for only the first result. 若要检索多个结果的架构信息,请使用 Fill,并将 MissingSchemaAction 设置为 AddWithKeyTo retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey.

当使用 FillSchema 时,用于 SQL Server 的 .NET Framework 数据提供程序将 FOR BROWSE 子句追加到正在执行的语句。When using FillSchema, the .NET Framework Data Provider for SQL Server appends a FOR BROWSE clause to the statement being executed. 用户应该注意潜在的副作用,例如对 SET FMTONLY ON 语句的使用产生的干扰。The user should be aware of potential side effects, such as interference with the use of SET FMTONLY ON statements. 有关详细信息,请参阅 SET FMTONLY (Transact-SQL)For more information, see SET FMTONLY (Transact-SQL).

继承者说明

FillSchema(DataSet, SchemaType) 方法的实现是受保护的,旨在供 .NET Framework 数据提供程序使用。This implementation of the FillSchema(DataSet, SchemaType) method is protected and is designed for use by a .NET Framework data provider.

另请参阅

FillSchema(DataTable, SchemaType, IDbCommand, CommandBehavior)

根据指定的 DataTable、命令字符串以及 SchemaType 值配置指定 CommandBehavior 的架构。Configures the schema of the specified DataTable based on the specified SchemaType, command string, and CommandBehavior values.

protected:
 virtual System::Data::DataTable ^ FillSchema(System::Data::DataTable ^ dataTable, System::Data::SchemaType schemaType, System::Data::IDbCommand ^ command, System::Data::CommandBehavior behavior);
protected virtual System.Data.DataTable FillSchema (System.Data.DataTable dataTable, System.Data.SchemaType schemaType, System.Data.IDbCommand command, System.Data.CommandBehavior behavior);
override this.FillSchema : System.Data.DataTable * System.Data.SchemaType * System.Data.IDbCommand * System.Data.CommandBehavior -> System.Data.DataTable

参数

dataTable
DataTable

要用数据源中的架构填充的 DataTableThe DataTable to be filled with the schema from the data source.

schemaType
SchemaType

SchemaType 值之一。One of the SchemaType values.

command
IDbCommand

用于从数据源中检索行的 SQL SELECT 语句。The SQL SELECT statement used to retrieve rows from the data source.

behavior
CommandBehavior

CommandBehavior 值之一。One of the CommandBehavior values.

返回

包含从数据源返回的架构信息的 DataTable 对象。A of DataTable object that contains schema information returned from the data source.

注解

FillSchema 方法使用 SelectCommand从数据源检索架构。The FillSchema method retrieves the schema from the data source using the SelectCommand. SelectCommand 关联的连接对象必须有效,但不需要打开。The connection object associated with the SelectCommand must be valid, but it does not need to be open. 如果在调用 FillSchema 之前关闭连接,则会打开该连接,以检索数据,然后关闭。If the connection is closed before FillSchema is called, it is opened to retrieve data, then closed. 如果在调用 FillSchema 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before FillSchema is called, it remains open.

FillSchema 操作向目标 DataSet添加 DataTableA FillSchema operation adds a DataTable to the destination DataSet. 然后,将列添加到 DataTableDataColumnCollection 中,并配置以下 DataColumn 属性(如果它们存在于数据源中):It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source:

FillSchema 还根据以下规则配置 PrimaryKeyConstraints 属性:FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:

  • 如果 SelectCommand返回一个或多个主键列,则将它们用作 DataTable的主键列。If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable.

  • 如果未返回主键列但唯一列为,则当且仅当不可 null 所有唯一列时,才将唯一列用作主键。If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. 如果任何列可为 null,则会将 UniqueConstraint 添加到 ConstraintCollection,但不会设置 PrimaryKey 属性。If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set.

  • 如果为主键列和唯一列都返回,则主键列将用作 DataTable的主键列。If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable.

请注意,根据前面的规则将主键和 unique 约束添加到 ConstraintCollection 中,但不会添加其他约束类型。Note that primary keys and unique constraints are added to the ConstraintCollection according to the preceding rules, but other constraint types are not added.

如果在 SQL Server 表中的一列或多列上定义了唯一聚集索引,并且在一组单独的列上定义了 primary key 约束,则将返回聚集索引中的列的名称。If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. 若要返回主键列的名称,请使用带有指定主键索引名称的 SELECT 语句的查询提示。To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. 有关指定查询提示的详细信息,请参阅提示(transact-sql)-查询For more information about specifying query hints, see Hints (Transact-SQL) - Query.

如果 IDataAdapter 在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等来为后续列生成名称。If the IDataAdapter encounters duplicate columns while populating a DataTable, it generates names for the subsequent columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. 如果传入数据包含未命名列,则这些列将按照模式 "Column1"、"Column2" 等放置在 DataSet 中。If the incoming data contains unnamed columns, they are placed in the DataSet according to the pattern "Column1", "Column2", and so on. 当将多个结果集添加到 DataSet 时,每个结果集将放在一个单独的表中。When multiple result sets are added to the DataSet each result set is placed in a separate table. 附加的结果集通过将整数值追加到指定的表名来命名(例如,"表"、"Table1"、"Table2" 等)。Additional result sets are named by appending integral values to the specified table name (for example, "Table", "Table1", "Table2", and so on.). 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

FillSchema 不返回任何行。FillSchema does not return any rows. 使用 Fill 方法将行添加到 DataTable中。Use the Fill method to add rows to a DataTable.

备注

当处理返回多个结果的批处理 SQL 语句时,OLE DB 的 .NET Framework 数据提供程序 FillSchema 的实现仅检索第一个结果的架构信息。When handling batch SQL statements that return multiple results, the implementation of FillSchema for the .NET Framework Data Provider for OLE DB retrieves schema information for only the first result. 若要检索多个结果的架构信息,请使用 Fill,并将 MissingSchemaAction 设置为 AddWithKeyTo retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey.

当使用 FillSchema 时,用于 SQL Server 的 .NET Framework 数据提供程序将 FOR BROWSE 子句追加到正在执行的语句。When using FillSchema, the .NET Framework Data Provider for SQL Server appends a FOR BROWSE clause to the statement being executed. 用户应该注意潜在的副作用,例如对 SET FMTONLY ON 语句的使用产生的干扰。The user should be aware of potential side effects, such as interference with the use of SET FMTONLY ON statements. 有关详细信息,请参阅 SET FMTONLY (Transact-SQL)For more information, see SET FMTONLY (Transact-SQL).

继承者说明

FillSchema(DataSet, SchemaType) 方法的实现是受保护的,旨在供 .NET Framework 数据提供程序使用。This implementation of the FillSchema(DataSet, SchemaType) method is protected and is designed for use by a .NET Framework data provider.

另请参阅

FillSchema(DataSet, SchemaType, String)

DataTable 添加到指定的 DataSet 中,并根据指定的 SchemaTypeDataTable 配置架构以匹配数据源中的架构。Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based upon the specified SchemaType and DataTable.

public:
 cli::array <System::Data::DataTable ^> ^ FillSchema(System::Data::DataSet ^ dataSet, System::Data::SchemaType schemaType, System::String ^ srcTable);
public System.Data.DataTable[] FillSchema (System.Data.DataSet dataSet, System.Data.SchemaType schemaType, string srcTable);
override this.FillSchema : System.Data.DataSet * System.Data.SchemaType * string -> System.Data.DataTable[]

参数

dataSet
DataSet

要插入架构的 DataSetA DataSet to insert the schema in.

schemaType
SchemaType

SchemaType 值之一,指定如何插入架构。One of the SchemaType values that specify how to insert the schema.

srcTable
String

用于表映射的源表的名称。The name of the source table to use for table mapping.

返回

一个引用,指向添加到 DataTableDataSet 对象的集合。A reference to a collection of DataTable objects that were added to the DataSet.

异常

找不到从中获取架构的源表。A source table from which to get the schema could not be found.

示例

下面的示例使用派生类 SqlDataAdapter来使用架构填充 DataSet,并返回 DataSetThe following example uses the derived class, SqlDataAdapter, to fill a DataSet with the schema, and returns a DataSet.

public static DataSet GetCustomerData(string dataSetName,
    string connectionString)
{
    DataSet dataSet = new DataSet(dataSetName);

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter(
            "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", connection);

        DataTableMapping mapping = adapter.TableMappings.Add("Table", "Customers");
        mapping.ColumnMappings.Add("CompanyName", "Name");
        mapping.ColumnMappings.Add("ContactName", "Contact");

        connection.Open();

        adapter.FillSchema(dataSet, SchemaType.Source, "Customers");
        adapter.Fill(dataSet);

        return dataSet;
    }
}
Private Function GetCustomerData(ByVal dataSetName As String, _
    ByVal connectionString As String) As DataSet

    Dim dataSet As New DataSet(dataSetName)

    Using connection As SqlConnection = New SqlConnection(connectionString)

        Dim adapter As New SqlDataAdapter( _
           "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", _
           connection)

        Dim mapping As DataTableMapping = adapter.TableMappings.Add( _
           "Table", "Customers")
        mapping.ColumnMappings.Add("CompanyName", "Name")
        mapping.ColumnMappings.Add("ContactName", "Contact")

        connection.Open()

        adapter.FillSchema(dataSet, SchemaType.Source, "Customers")
        adapter.Fill(dataSet)
        Return dataSet
    End Using
End Function

注解

此方法使用 SelectCommand从数据源中检索架构信息。This method retrieves the schema information from the data source using the SelectCommand.

FillSchema 操作向目标 DataSet添加 DataTableA FillSchema operation adds a DataTable to the destination DataSet. 然后,将列添加到 DataTableDataColumnCollection 中,并配置以下 DataColumn 属性(如果它们存在于数据源中):It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source:

FillSchema 还根据以下规则配置 PrimaryKeyConstraints 属性:FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:

  • 如果 SelectCommand返回一个或多个主键列,则将它们用作 DataTable的主键列。If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable.

  • 如果未返回主键列但唯一列为,则当且仅当不可 null 所有唯一列时,才将唯一列用作主键。If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. 如果任何列可为 null,则会将 UniqueConstraint 添加到 ConstraintCollection,但不会设置 PrimaryKey 属性。If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set.

  • 如果为主键列和唯一列都返回,则主键列将用作 DataTable的主键列。If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable.

请注意,根据前面的规则将主键和 unique 约束添加到 ConstraintCollection 中,但不会添加其他约束类型。Note that primary keys and unique constraints are added to the ConstraintCollection according to the preceding rules, but other constraint types are not added.

如果在 SQL Server 表中的一列或多列上定义了唯一聚集索引,并且在一组单独的列上定义了 primary key 约束,则将返回聚集索引中的列的名称。If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. 若要返回主键列的名称,请使用带有指定主键索引名称的 SELECT 语句的查询提示。To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. 有关指定查询提示的详细信息,请参阅提示(transact-sql)-查询For more information about specifying query hints, see Hints (Transact-SQL) - Query.

Fill 期间使用主键信息查找并替换其键列匹配的任何行。Primary key information is used during Fill to find and replace any rows whose key columns match. 如果这不是所需的行为,请使用 Fill,而不请求架构信息。If this is not the desired behavior, use Fill without requesting schema information.

如果 DbDataAdapter 在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等来为后续列生成名称。If the DbDataAdapter encounters duplicate columns while populating a DataTable, it generates names for the subsequent columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. 如果传入数据包含未命名列,则这些列将按照模式 "Column1"、"Column2" 等放置在 DataSet 中。If the incoming data contains unnamed columns, they are placed in the DataSet according to the pattern "Column1", "Column2", and so on. 当将多个结果集添加到 DataSet 时,每个结果集将放在一个单独的表中。When multiple result sets are added to the DataSet each result set is placed in a separate table. 附加的结果集通过将整数值追加到指定的表名来命名(例如,"表"、"Table1"、"Table2" 等)。Additional result sets are named by appending integral values to the specified table name (for example, "Table", "Table1", "Table2", and so on.). 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

FillSchema 方法支持这样的方案: DataSet 包含多个 DataTable 对象,这些对象的名称仅区分大小写。The FillSchema method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 在这种情况下,FillSchema 执行区分大小写的比较来查找相应的表,如果不存在完全匹配项,则创建新表。In such situations, FillSchema performs a case-sensitive comparison to find the corresponding table, and creates a new table if no exact match exists. 下面C#的代码演示了此行为。The following C# code illustrates this behavior.

DataSet dataset = new DataSet();  
dataset.Tables.Add("aaa");  
dataset.Tables.Add("AAA");  
adapter.FillSchema(dataset, "aaa"); // Fills the schema of "aaa", which already exists in the DataSet.  
adapter.FillSchema(dataset, "Aaa"); // Adds a new table called "Aaa".  

如果调用 FillSchemaDataSet 只包含一个名称只是大小写不同的 DataTable,则将更新该 DataTableIf FillSchema is called and the DataSet contains only one DataTable whose name differs only by case, that DataTable is updated. 在这种情况下,比较不区分大小写。In this scenario, the comparison is case insensitive. 下面C#的代码演示了此行为。The following C# code illustrates this behavior.

DataSet dataset = new DataSet();  
dataset.Tables.Add("aaa");  
adapter.FillSchema(dataset, "AAA"); // Fills the schema of table "aaa" because only one similarly named table is in the DataSet.  

与 select 命令关联的 IDbConnection 对象必须有效,但不需要打开。The IDbConnection object associated with the select command must be valid, but it does not need to open. 如果 IDbConnectionFillSchema 调用之前关闭,则将其打开以检索数据,然后关闭。If the IDbConnection is closed before FillSchema is called, it is opened to retrieve data, then closed. 如果在调用 FillSchema 之前连接处于打开状态,则该连接会保持打开状态。If the connection is open before FillSchema is called, it is left open.

备注

当处理返回多个结果的批处理 SQL 语句时,OLE DB 的 .NET Framework 数据提供程序 FillSchema 的实现仅检索第一个结果的架构信息。When handling batch SQL statements that return multiple results, the implementation of FillSchema for the .NET Framework Data Provider for OLE DB retrieves schema information for only the first result. 若要检索多个结果的架构信息,请使用 Fill,并将 MissingSchemaAction 设置为 AddWithKeyTo retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey.

当使用 FillSchema 时,用于 SQL Server 的 .NET Framework 数据提供程序将 FOR BROWSE 子句追加到正在执行的语句。When using FillSchema, the .NET Framework Data Provider for SQL Server appends a FOR BROWSE clause to the statement being executed. 用户应该注意潜在的副作用,例如对 SET FMTONLY ON 语句的使用产生的干扰。The user should be aware of potential side effects, such as interference with the use of SET FMTONLY ON statements. 有关详细信息,请参阅 SET FMTONLY (Transact-SQL)For more information, see SET FMTONLY (Transact-SQL).

另请参阅

FillSchema(DataTable, SchemaType)

根据指定的 DataTable 配置指定 SchemaType 的架构。Configures the schema of the specified DataTable based on the specified SchemaType.

public:
 System::Data::DataTable ^ FillSchema(System::Data::DataTable ^ dataTable, System::Data::SchemaType schemaType);
public System.Data.DataTable FillSchema (System.Data.DataTable dataTable, System.Data.SchemaType schemaType);
override this.FillSchema : System.Data.DataTable * System.Data.SchemaType -> System.Data.DataTable

参数

dataTable
DataTable

要用数据源中的架构填充的 DataTableThe DataTable to be filled with the schema from the data source.

schemaType
SchemaType

SchemaType 值之一。One of the SchemaType values.

返回

一个 DataTable,其中包含从数据源返回的架构信息。A DataTable that contains schema information returned from the data source.

示例

下面的示例使用派生类 SqlDataAdapter来使用架构填充 DataSet,并返回 DataTableThe following example uses the derived class, SqlDataAdapter, to fill a DataSet with the schema, and returns a DataTable.

public static DataTable GetCustomerData(string dataSetName,
    string connectionString)
{
    DataTable table = new DataTable(dataSetName);

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter(
            "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", connection);

        DataTableMapping mapping = adapter.TableMappings.Add("Table", "Customers");
        mapping.ColumnMappings.Add("CompanyName", "Name");
        mapping.ColumnMappings.Add("ContactName", "Contact");

        connection.Open();

        adapter.FillSchema(table, SchemaType.Mapped);
        adapter.Fill(table);
        return table;
    }
}
Private Function GetCustomerData(ByVal dataTableName As String, _
    ByVal connectionString As String) As DataTable

    Dim table As New DataTable(dataTableName)

    Using connection As SqlConnection = New SqlConnection(connectionString)

        Dim adapter New SqlDataAdapter( _
           "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", _
           connection)

        Dim mapping As DataTableMapping = adapter.TableMappings.Add( _
           "Table", "Customers")
        mapping.ColumnMappings.Add("CompanyName", "Name")
        mapping.ColumnMappings.Add("ContactName", "Contact")

        connection.Open()

        adapter.FillSchema(table, SchemaType.Mapped)
        adapter.Fill(table)
        Return table
    End Using
End Function

注解

FillSchema 方法使用 SelectCommand从数据源检索架构。The FillSchema method retrieves the schema from the data source using the SelectCommand. SelectCommand 关联的连接对象必须有效,但不需要打开。The connection object associated with the SelectCommand must be valid, but it does not need to be open. 如果在调用 FillSchema 之前关闭连接,则会打开该连接,以检索数据,然后关闭。If the connection is closed before FillSchema is called, it is opened to retrieve data, then closed. 如果在调用 FillSchema 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before FillSchema is called, it remains open.

FillSchema 操作返回一个 DataTableA FillSchema operation returns a DataTable. 然后,将列添加到 DataTableDataColumnCollection 中,并配置以下 DataColumn 属性(如果它们存在于数据源中):It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source:

FillSchema 还根据以下规则配置 PrimaryKeyConstraints 属性:FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:

  • 如果已为 DataTable定义了 PrimaryKey,或者 DataTable 包含数据,则不会设置 PrimaryKey 属性。If a PrimaryKey has already been defined for the DataTable, or the DataTable contains data, the PrimaryKey property will not be set.

  • 如果 SelectCommand返回一个或多个主键列,则将它们用作 DataTable的主键列。If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable.

  • 如果未返回主键列但唯一列为,则当且仅当不可 null 所有唯一列时,才将唯一列用作主键。If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. 如果任何列可为 null,则会将 UniqueConstraint 添加到 ConstraintCollection,但不会设置 PrimaryKey 属性。If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set.

  • 如果为主键列和唯一列都返回,则主键列将用作 DataTable的主键列。If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable.

请注意,根据前面的规则将主键和 unique 约束添加到 ConstraintCollection 中,但不会添加其他约束类型。Note that primary keys and unique constraints are added to the ConstraintCollection according to the preceding rules, but other constraint types are not added. 此过程可能需要多次往返服务器。This process may require several round-trips to the server.

如果在 SQL Server 表中的一列或多列上定义了唯一聚集索引,并且在一组单独的列上定义了 primary key 约束,则将返回聚集索引中的列的名称。If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. 若要返回主键列的名称,请使用带有指定主键索引名称的 SELECT 语句的查询提示。To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. 有关指定查询提示的详细信息,请参阅提示(transact-sql)-查询For more information about specifying query hints, see Hints (Transact-SQL) - Query.

如果 DbDataAdapter 在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等来为后续列生成名称。If the DbDataAdapter encounters duplicate columns while populating a DataTable, it generates names for the subsequent columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. 如果传入数据包含未命名列,则这些列将按照模式 "Column1"、"Column2" 等放置在 DataSet 中。If the incoming data contains unnamed columns, they are placed in the DataSet according to the pattern "Column1", "Column2", and so on. 当将多个结果集添加到 DataSet 时,每个结果集将放在一个单独的表中。When multiple result sets are added to the DataSet each result set is placed in a separate table. 附加的结果集通过将整数值追加到指定的表名来命名(例如,"表"、"Table1"、"Table2" 等)。Additional result sets are named by appending integral values to the specified table name (for example, "Table", "Table1", "Table2", and so on.). 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

FillSchema 不返回任何行。FillSchema does not return any rows. 使用 Fill 方法将行添加到 DataTable中。Use the Fill method to add rows to a DataTable.

备注

当处理返回多个结果的批处理 SQL 语句时,OLE DB 的 .NET Framework 数据提供程序 FillSchema 的实现仅检索第一个结果的架构信息。When handling batch SQL statements that return multiple results, the implementation of FillSchema for the .NET Framework Data Provider for OLE DB retrieves schema information for only the first result. 若要检索多个结果的架构信息,请使用 Fill,并将 MissingSchemaAction 设置为 AddWithKeyTo retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey.

当使用 FillSchema 时,用于 SQL Server 的 .NET Framework 数据提供程序将 FOR BROWSE 子句追加到正在执行的语句。When using FillSchema, the .NET Framework Data Provider for SQL Server appends a FOR BROWSE clause to the statement being executed. 用户应该注意潜在的副作用,例如对 SET FMTONLY ON 语句的使用产生的干扰。The user should be aware of potential side effects, such as interference with the use of SET FMTONLY ON statements. 有关详细信息,请参阅 SET FMTONLY (Transact-SQL)For more information, see SET FMTONLY (Transact-SQL).

另请参阅

FillSchema(DataSet, SchemaType)

将名为“Table”的 DataTable 添加到指定的 DataSet,并根据指定的 SchemaType 配置架构以匹配数据源中的架构。Adds a DataTable named "Table" to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType.

public:
 override cli::array <System::Data::DataTable ^> ^ FillSchema(System::Data::DataSet ^ dataSet, System::Data::SchemaType schemaType);
public override System.Data.DataTable[] FillSchema (System.Data.DataSet dataSet, System.Data.SchemaType schemaType);
override this.FillSchema : System.Data.DataSet * System.Data.SchemaType -> System.Data.DataTable[]

参数

dataSet
DataSet

要插入架构的 DataSetA DataSet to insert the schema in.

schemaType
SchemaType

SchemaType 值之一,指定如何插入架构。One of the SchemaType values that specify how to insert the schema.

返回

一个引用,指向添加到 DataTableDataSet 对象的集合。A reference to a collection of DataTable objects that were added to the DataSet.

实现

示例

下面的示例使用派生类 SqlDataAdapter来使用架构填充 DataSet,并返回 DataSetThe following example uses the derived class, SqlDataAdapter, to fill a DataSet with the schema, and returns a DataSet.

public static DataSet GetCustomerData(string dataSetName,
    string connectionString)
{
    DataSet dataSet = new DataSet(dataSetName);

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter(
            "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", connection);

        DataTableMapping mapping = adapter.TableMappings.Add("Table", "Customers");
        mapping.ColumnMappings.Add("CompanyName", "Name");
        mapping.ColumnMappings.Add("ContactName", "Contact");

        connection.Open();

        adapter.FillSchema(dataSet, SchemaType.Mapped);
        adapter.Fill(dataSet);

        return dataSet;
    }
}
Private Function GetCustomerData(ByVal dataSetName As String, _
    ByVal connectionString As String) As DataSet

    Dim dataSet As New DataSet(dataSetName)

    Using connection As SqlConnection = New SqlConnection(connectionString)
        Dim adapter As New SqlDataAdapter( _
           "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", _
           connection)

        Dim mapping As DataTableMapping = adapter.TableMappings.Add( _
           "Table", "Customers")
        mapping.ColumnMappings.Add("CompanyName", "Name")
        mapping.ColumnMappings.Add("ContactName", "Contact")

        connection.Open()

        adapter.FillSchema(dataSet, SchemaType.Mapped)
        adapter.Fill(dataSet)
        Return dataSet
    End Using
End Function

注解

此方法使用 SelectCommand从数据源中检索架构信息。This method retrieves the schema information from the data source using the SelectCommand.

FillSchema 操作向目标 DataSet添加 DataTableA FillSchema operation adds a DataTable to the destination DataSet. 然后,将列添加到 DataTableDataColumnCollection 中,并配置以下 DataColumn 属性(如果它们存在于数据源中):It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source:

FillSchema 还根据以下规则配置 PrimaryKeyConstraints 属性:FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:

  • 如果 SelectCommand返回一个或多个主键列,则将它们用作 DataTable的主键列。If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable.

  • 如果未返回主键列但唯一列为,则当且仅当不可 null 所有唯一列时,才将唯一列用作主键。If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. 如果任何列可为 null,则会将 UniqueConstraint 添加到 ConstraintCollection,但不会设置 PrimaryKey 属性。If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set.

  • 如果为主键列和唯一列都返回,则主键列将用作 DataTable的主键列。If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable.

请注意,根据前面的规则将主键和 unique 约束添加到 ConstraintCollection 中,但不会添加其他约束类型。Note that primary keys and unique constraints are added to the ConstraintCollection according to the preceding rules, but other constraint types are not added.

如果在 SQL Server 表中的一列或多列上定义了唯一聚集索引,并且在一组单独的列上定义了 primary key 约束,则将返回聚集索引中的列的名称。If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. 若要返回主键列的名称,请使用带有指定主键索引名称的 SELECT 语句的查询提示。To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. 有关指定查询提示的详细信息,请参阅提示(transact-sql)-查询For more information about specifying query hints, see Hints (Transact-SQL) - Query.

Fill 期间使用主键信息查找并替换其键列匹配的任何行。Primary key information is used during Fill to find and replace any rows whose key columns match. 如果这不是所需的行为,请使用 Fill,而不请求架构信息。If this is not the desired behavior, use Fill without requesting schema information.

如果 IDataAdapter 在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等来为后续列生成名称。If the IDataAdapter encounters duplicate columns while populating a DataTable, it generates names for the subsequent columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. 如果传入数据包含未命名列,则这些列将按照模式 "Column1"、"Column2" 等放置在 DataSet 中。If the incoming data contains unnamed columns, they are placed in the DataSet according to the pattern "Column1", "Column2", and so on. 当将多个结果集添加到 DataSet 时,每个结果集将放在一个单独的表中。When multiple result sets are added to the DataSet each result set is placed in a separate table. 附加的结果集通过将整数值追加到指定的表名来命名(例如,"表"、"Table1"、"Table2" 等)。Additional result sets are named by appending integral values to the specified table name (for example, "Table", "Table1", "Table2", and so on.). 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

与 select 命令关联的 IDbConnection 对象必须有效,但不需要打开。The IDbConnection object associated with the select command must be valid, but it does not need to open. 如果 IDbConnectionFillSchema 调用之前关闭,则将其打开以检索数据,然后关闭。If the IDbConnection is closed before FillSchema is called, it is opened to retrieve data, then closed. 如果在调用 FillSchema 之前连接处于打开状态,则该连接会保持打开状态。If the connection is open before FillSchema is called, it is left open.

备注

当处理返回多个结果的批处理 SQL 语句时,OLE DB 的 .NET Framework 数据提供程序 FillSchema 的实现仅检索第一个结果的架构信息。When handling batch SQL statements that return multiple results, the implementation of FillSchema for the .NET Framework Data Provider for OLE DB retrieves schema information for only the first result. 若要检索多个结果的架构信息,请使用 Fill,并将 MissingSchemaAction 设置为 AddWithKeyTo retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey.

当使用 FillSchema 时,用于 SQL Server 的 .NET Framework 数据提供程序将 FOR BROWSE 子句追加到正在执行的语句。When using FillSchema, the .NET Framework Data Provider for SQL Server appends a FOR BROWSE clause to the statement being executed. 用户应该注意潜在的副作用,例如对 SET FMTONLY ON 语句的使用产生的干扰。The user should be aware of potential side effects, such as interference with the use of SET FMTONLY ON statements. 有关详细信息,请参阅 SET FMTONLY (Transact-SQL)For more information, see SET FMTONLY (Transact-SQL).

另请参阅

适用于