DbDataAdapter.Fill 方法

定义

填充 DataSetDataTableFills a DataSet or a DataTable.

重载

Fill(DataSet, Int32, Int32, String, IDbCommand, CommandBehavior)

使用 DataSet 和源表名称、命令字符串以及命令行为,在 DataSet 的指定范围中添加或刷新某些行以使它们与数据源中对应的行相一致。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and source table names, command string, and command behavior.

Fill(DataTable[], Int32, Int32, IDbCommand, CommandBehavior)

DataSet 的指定范围中添加或刷新行以匹配使用 DataSetDataTable 名称的数据源中的行。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names.

Fill(DataSet, String, IDataReader, Int32, Int32)

DataSet 的指定范围中添加或刷新行以与使用 DataSetDataTableIDataReader 名称的数据源中的行进行匹配。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet, DataTable, and IDataReader names.

Fill(DataSet, Int32, Int32, String)

DataSet 的指定范围中添加或刷新行以匹配使用 DataSetDataTable 名称的数据源中的行。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names.

Fill(Int32, Int32, DataTable[])

DataTable 中添加或刷新行,以与从指定的记录开始一直检索到指定的最大数目的记录的数据源中的行匹配。Adds or refreshes rows in a DataTable to match those in the data source starting at the specified record and retrieving up to the specified maximum number of records.

Fill(DataTable)

DataSet 的指定范围中添加或刷新行,以与使用 DataTable 名称的数据源中的行匹配。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataTable name.

Fill(DataTable, IDataReader)

DataTable 中添加或刷新行,以与使用指定的 DataTableIDataReader 名称的数据源中的行匹配。Adds or refreshes rows in a DataTable to match those in the data source using the specified DataTable and IDataReader names.

Fill(DataSet, String)

DataSet 中添加或刷新行以匹配使用 DataSetDataTable 名称的数据源中的行。Adds or refreshes rows in the DataSet to match those in the data source using the DataSet and DataTable names.

Fill(DataSet)

DataSet 中添加或刷新行。Adds or refreshes rows in the DataSet.

Fill(DataTable, IDbCommand, CommandBehavior)

DataTable 中添加或刷新行,以与使用指定的 DataTableIDbCommandCommandBehavior 的数据源中的行匹配。Adds or refreshes rows in a DataTable to match those in the data source using the specified DataTable, IDbCommand and CommandBehavior.

Fill(DataSet, Int32, Int32, String, IDbCommand, CommandBehavior)

使用 DataSet 和源表名称、命令字符串以及命令行为,在 DataSet 的指定范围中添加或刷新某些行以使它们与数据源中对应的行相一致。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and source table names, command string, and command behavior.

protected:
 virtual int Fill(System::Data::DataSet ^ dataSet, int startRecord, int maxRecords, System::String ^ srcTable, System::Data::IDbCommand ^ command, System::Data::CommandBehavior behavior);
protected virtual int Fill (System.Data.DataSet dataSet, int startRecord, int maxRecords, string srcTable, System.Data.IDbCommand command, System.Data.CommandBehavior behavior);
override this.Fill : System.Data.DataSet * int * int * string * System.Data.IDbCommand * System.Data.CommandBehavior -> int
Protected Overridable Function Fill (dataSet As DataSet, startRecord As Integer, maxRecords As Integer, srcTable As String, command As IDbCommand, behavior As CommandBehavior) As Integer

参数

dataSet
DataSet

要用记录和架构(如果必要)填充的 DataSetA DataSet to fill with records and, if necessary, schema.

startRecord
Int32

从其开始的从零开始的记录号。The zero-based record number to start with.

maxRecords
Int32

要检索的最大记录数。The maximum number of records to retrieve.

srcTable
String

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

command
IDbCommand

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

behavior
CommandBehavior

CommandBehavior 值之一。One of the CommandBehavior values.

返回

已在 DataSet 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataSet. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

例外

源表无效。The source table is invalid.

startRecord 参数小于 0。The startRecord parameter is less than 0.

-或--or- maxRecords 参数小于 0。The maxRecords parameter is less than 0.

注解

Fill 方法使用关联的 SelectCommand 属性指定的 SELECT 语句从数据源中检索行。The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. 与 SELECT 语句关联的连接对象必须有效,但不需要打开。The connection object associated with the SELECT statement must be valid, but it does not need to be open. 如果在调用 Fill 之前关闭连接,则会打开该连接,以检索数据,然后关闭。If the connection is closed before Fill is called, it is opened to retrieve data, then closed. 如果在调用 Fill 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before Fill is called, it remains open.

然后 Fill 操作将行添加到 DataSet中的目标 DataTable 对象,并创建 DataTable 对象(如果它们尚不存在)。The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist. 创建 DataTable 对象时,Fill 操作通常只创建列名称元数据。When creating DataTable objects, the Fill operation normally creates only column name metadata. 但是,如果 MissingSchemaAction 属性设置为 AddWithKey,则还会创建相应的主键和约束。However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

如果 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.

Fill 方法支持这样的方案: DataSet 包含多个 DataTable 对象,这些对象的名称仅区分大小写。The Fill method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 在这种情况下,Fill 执行区分大小写的比较来查找相应的表,如果不存在完全匹配项,则创建新表。In such situations, Fill 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.Fill(dataset, "aaa"); // Fills "aaa", which already exists in the DataSet.  
adapter.Fill(dataset, "Aaa"); // Adds a new table called "Aaa".  

如果调用 FillDataSet 只包含一个名称只是大小写不同的 DataTable,则将更新该 DataTableIf Fill 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.Fill(dataset, "AAA"); // Fills table "aaa" because only one similarly named table is in the DataSet.  

您可以在同一 DataTable上多次使用 Fill 方法。You can use the Fill method multiple times on the same DataTable. 如果存在主键,则传入行与已存在的匹配行合并在一起。If a primary key exists, incoming rows are merged with matching rows that already exist. 如果主键不存在,将在 DataTable中追加传入行。If no primary key exists, incoming rows are appended to the DataTable.

如果 SelectCommand 返回 OUTER JOIN 的结果,则 DataAdapter 不会为生成的 PrimaryKey 设置 DataTable值。If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. 必须显式定义主键,以确保正确解析重复行。You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

备注

当处理返回多个结果的批处理 SQL 语句时,.NET Framework 数据提供程序的 FillFillSchema 的实现仅检索第一个结果的架构信息。When handling batch SQL statements that return multiple results, the implementation of Fill and FillSchema for a .NET Framework data provider retrieves schema information for only the first result.

继承者说明

Fill(DataSet) 方法的此重载受保护,旨在供 .NET Framework 数据提供程序使用。This overload of the Fill(DataSet) method is protected and is designed for use by a .NET Framework data provider.

另请参阅

Fill(DataTable[], Int32, Int32, IDbCommand, CommandBehavior)

DataSet 的指定范围中添加或刷新行以匹配使用 DataSetDataTable 名称的数据源中的行。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names.

protected:
 virtual int Fill(cli::array <System::Data::DataTable ^> ^ dataTables, int startRecord, int maxRecords, System::Data::IDbCommand ^ command, System::Data::CommandBehavior behavior);
protected virtual int Fill (System.Data.DataTable[] dataTables, int startRecord, int maxRecords, System.Data.IDbCommand command, System.Data.CommandBehavior behavior);
override this.Fill : System.Data.DataTable[] * int * int * System.Data.IDbCommand * System.Data.CommandBehavior -> int
Protected Overridable Function Fill (dataTables As DataTable(), startRecord As Integer, maxRecords As Integer, command As IDbCommand, behavior As CommandBehavior) As Integer

参数

dataTables
DataTable[]

要从数据源填充的 DataTable 对象。The DataTable objects to fill from the data source.

startRecord
Int32

从其开始的从零开始的记录号。The zero-based record number to start with.

maxRecords
Int32

要检索的最大记录数。The maximum number of records to retrieve.

command
IDbCommand

执行以填充 IDbCommand 对象的 DataTableThe IDbCommand executed to fill the DataTable objects.

behavior
CommandBehavior

CommandBehavior 值之一。One of the CommandBehavior values.

返回

已在数据表中添加或刷新的行数。The number of rows added to or refreshed in the data tables.

例外

DataSet 无效。The DataSet is invalid.

源表无效。The source table is invalid.

-或--or-

connection 无效。The connection is invalid.

未能找到连接。The connection could not be found.

startRecord 参数小于 0。The startRecord parameter is less than 0.

-或--or- maxRecords 参数小于 0。The maxRecords parameter is less than 0.

注解

如果 maxRecords 值为0,则获取在开始记录后找到的所有记录。A maxRecords value of 0 gets all records found after the start record. 如果 maxRecords 大于剩余行数,则只返回剩余的行,并且不会发出错误。If maxRecords is greater than the number of remaining rows, only the remaining rows are returned and no error is issued.

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

如果命令未返回任何行,则不会将任何表添加到 DataSet中,但不会引发异常。If a command does not return any rows, no tables are added to the DataSet, but no exception is raised.

如果 DbDataAdapter 对象在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等为后续列生成名称。If the DbDataAdapter object encounters duplicate columns while populating a DataTable, it will generate 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.

当指定的查询返回多个结果时,每个结果集将放在一个单独的表中。When the query specified returns multiple results, 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). 由于没有为不返回行的查询创建表,因此,如果要处理一个 insert 查询后跟一个 select 查询,则为 select 查询创建的表将命名为 "Table",因为它是第一个创建的表。Since no table is created for a query that does not return rows, if you were to process an insert query followed by a select query, the table created for the select query would be named "Table", because it is the first table created. 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

Fill 方法支持这样的方案: DataSet 包含多个 DataTable 对象,这些对象的名称仅区分大小写。The Fill method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 在这种情况下,Fill 执行区分大小写的比较来查找相应的表,如果不存在完全匹配项,则创建新表。In such situations, Fill 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.Fill(dataset, "aaa"); // Fills "aaa", which already exists in the DataSet.  
adapter.Fill(dataset, "Aaa"); // Adds a new table called "Aaa".  

如果调用 FillDataSet 只包含一个名称只是大小写不同的 DataTable,则将更新该 DataTableIf Fill 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.Fill(dataset, "AAA"); // Fills table "aaa" because only one similarly named table is in the DataSet.  

如果在填充数据表时出现错误或异常,则在出现错误之前添加的行将保留在数据表中。If an error or an exception is encountered while populating the data tables, rows added prior to the occurrence of the error remain in the data tables. 操作的剩余部分中止。The remainder of the operation is aborted.

当用于填充 DataTable 对象的 SELECT 语句返回多个结果(如批处理 SQL 语句)时,请注意以下事项:When the SELECT statement used to populate the DataTable objects returns multiple results, such as a batch SQL statement, be aware of the following:

  • 处理来自批处理 SQL 语句的多个结果时,maxRecords 仅适用于第一个结果。When processing multiple results from a batch SQL statement, maxRecords only applies to the first result. 对于包含分章节结果的行也是如此(仅适用于 OLE DB 的 .NET Framework 数据提供程序)。The same is true for rows containing chaptered results (.NET Framework Data Provider for OLE DB only). 顶级结果受 maxRecords的限制,但会添加所有子行。The top level result is limited by maxRecords, but all child rows are added.

  • 如果其中一个结果包含错误,则会跳过所有后续结果。If one of the results contains an error, all subsequent results are skipped.

备注

DataSet 将不包含 maxRecords所指示的记录数。The DataSet will not contain more than the number of records indicated by maxRecords. 但仍从服务器返回查询生成的整个结果集。However, the entire resultset generated by the query is still returned from the server.

继承者说明

当在派生类中重写 Fill(DataSet) 时,请确保调用基类的 Fill(DataSet) 方法。When overriding Fill(DataSet) in a derived class, be sure to call the base class's Fill(DataSet) method.

另请参阅

Fill(DataSet, String, IDataReader, Int32, Int32)

DataSet 的指定范围中添加或刷新行以与使用 DataSetDataTableIDataReader 名称的数据源中的行进行匹配。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet, DataTable, and IDataReader names.

protected:
 virtual int Fill(System::Data::DataSet ^ dataSet, System::String ^ srcTable, System::Data::IDataReader ^ dataReader, int startRecord, int maxRecords);
protected virtual int Fill (System.Data.DataSet dataSet, string srcTable, System.Data.IDataReader dataReader, int startRecord, int maxRecords);
override this.Fill : System.Data.DataSet * string * System.Data.IDataReader * int * int -> int
Protected Overridable Function Fill (dataSet As DataSet, srcTable As String, dataReader As IDataReader, startRecord As Integer, maxRecords As Integer) As Integer

参数

dataSet
DataSet

要用记录和架构(如果必要)填充的 DataSetA DataSet to fill with records and, if necessary, schema.

srcTable
String

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

dataReader
IDataReader

IDataReader 的名称。The name of the IDataReader.

startRecord
Int32

从其开始的从零开始的记录号。The zero-based record number to start with.

maxRecords
Int32

要检索的最大记录数。The maximum number of records to retrieve.

返回

已在 DataSet 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataSet. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

Fill(DataSet, Int32, Int32, String)

DataSet 的指定范围中添加或刷新行以匹配使用 DataSetDataTable 名称的数据源中的行。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names.

public:
 int Fill(System::Data::DataSet ^ dataSet, int startRecord, int maxRecords, System::String ^ srcTable);
public int Fill (System.Data.DataSet dataSet, int startRecord, int maxRecords, string srcTable);
override this.Fill : System.Data.DataSet * int * int * string -> int
Public Function Fill (dataSet As DataSet, startRecord As Integer, maxRecords As Integer, srcTable As String) As Integer

参数

dataSet
DataSet

要用记录和架构(如果必要)填充的 DataSetA DataSet to fill with records and, if necessary, schema.

startRecord
Int32

从其开始的从零开始的记录号。The zero-based record number to start with.

maxRecords
Int32

要检索的最大记录数。The maximum number of records to retrieve.

srcTable
String

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

返回

已在 DataSet 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataSet. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

例外

DataSet 无效。The DataSet is invalid.

源表无效。The source table is invalid.

-或--or-

connection 无效。The connection is invalid.

未能找到连接。The connection could not be found.

startRecord 参数小于 0。The startRecord parameter is less than 0.

-或--or- maxRecords 参数小于 0。The maxRecords parameter is less than 0.

示例

下面的示例使用派生类 OleDbDataAdapter从 "类别" 表中填充15行(从第10行开始)的 DataSetThe following example uses the derived class, OleDbDataAdapter, to fill a DataSet with 15 rows, beginning at row 10, from the Categories table. 此示例假设已创建 OleDbDataAdapterDataSetThis example assumes that you have created an OleDbDataAdapter and a DataSet.

public void GetRecords()
{
    // ...
    // create dataSet and adapter
    // ...
    adapter.Fill(dataSet,9,15,"Categories");
}
Public Sub GetRecords()
    ' ...
    ' create dataSet and adapter
    ' ...
    adapter.Fill(dataSet, 9, 15, "Categories")
End Sub

注解

如果 maxRecords 值为0,则获取在开始记录后找到的所有记录。A maxRecords value of 0 gets all records found after the start record. 如果 maxRecords 大于剩余行数,则只返回剩余的行,并且不会发出错误。If maxRecords is greater than the number of remaining rows, only the remaining rows are returned, and no error is issued.

如果相应的 select 命令是返回多个结果的语句,Fill 仅将 maxRecords 应用于第一个结果。If the corresponding select command is a statement returning multiple results, Fill only applies maxRecords to the first result.

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

如果命令未返回任何行,则不会将任何表添加到 DataSet中,但不会引发异常。If a command does not return any rows, no tables are added to the DataSet, but no exception is raised.

如果 DbDataAdapter 对象在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等为后续列生成名称。If the DbDataAdapter object encounters duplicate columns while populating a DataTable, it will generate 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.

当指定的查询返回多个结果时,每个结果集将放在一个单独的表中。When the query specified returns multiple results, 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). 由于没有为不返回行的查询创建表,因此,如果处理的是 insert 查询后跟 select 查询,则为 select 查询创建的表名为 "Table",因为它是第一个创建的表。Because no table is created for a query that does not return rows, if you process an insert query followed by a select query, the table created for the select query is named "Table", because it is the first table created. 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

Fill 方法支持这样的方案: DataSet 包含多个 DataTable 对象,这些对象的名称仅区分大小写。The Fill method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 在这种情况下,Fill 执行区分大小写的比较来查找相应的表,如果不存在完全匹配项,则创建新表。In such situations, Fill 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.Fill(dataset, "aaa"); // Fills "aaa", which already exists in the DataSet.  
adapter.Fill(dataset, "Aaa"); // Adds a new table called "Aaa".  

如果调用 FillDataSet 只包含一个名称只是大小写不同的 DataTable,则将更新该 DataTableIf Fill 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.Fill(dataset, "AAA"); // Fills table "aaa" because only one similarly named table is in the DataSet.  

如果在填充数据表时出现错误或异常,则在出现错误之前添加的行将保留在数据表中。If an error or an exception is encountered while populating the data tables, rows added prior to the occurrence of the error remain in the data tables. 操作的剩余部分中止。The remainder of the operation is aborted.

当用于填充 DataSet 的 SELECT 语句返回多个结果(如批处理 SQL 语句)时,请注意以下事项:When the SELECT statement used to populate the DataSet returns multiple results, such as batch SQL statements, be aware of the following:

  • 处理来自批处理 SQL 语句的多个结果时,maxRecords 仅适用于第一个结果。When processing multiple results from a batch SQL statement, maxRecords only applies to the first result. 对于包含分章节结果的行也是如此(仅适用于 OLE DB 的 .NET Framework 数据提供程序)。The same is true for rows containing chaptered results (.NET Framework Data Provider for OLE DB only). 顶级结果受 maxRecords的限制,但会添加所有子行。The top level result is limited by maxRecords, but all child rows are added.

  • 如果其中一个结果包含错误,则会跳过所有后续结果,并且不会将其添加到 DataSet中。If one of the results contains an error, all subsequent results are skipped and not added to the DataSet.

使用后续 Fill 调用来刷新 DataSet的内容时,必须满足两个条件:When using subsequent Fill calls to refresh the contents of the DataSet, two conditions must be met:

  1. SQL 语句应与最初用于填充 DataSet的语句匹配。The SQL statement should match the one initially used to populate the DataSet.

  2. 列信息必须存在。The Key column information must be present.

如果存在主键信息,则会对所有重复行进行协调,并且仅在对应于 DataSetDataTable 中出现一次。If primary key information is present, any duplicate rows will be reconciled and only appear once in the DataTable that corresponds to the DataSet. 可以通过 FillSchema、指定 DataTablePrimaryKey 属性或将 MissingSchemaAction 属性设置为 AddWithKey来设置主键信息。Primary key information may be set either through FillSchema, by specifying the PrimaryKey property of the DataTable, or by setting the MissingSchemaAction property to AddWithKey.

如果 SelectCommand 返回 OUTER JOIN 的结果,则 DataAdapter 不会为生成的 PrimaryKey 设置 DataTable值。If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. 必须显式定义主键,以确保正确解析重复行。You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

备注

当处理返回多个结果的批处理 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.

备注

DataSet 将不包含 maxRecords所指示的记录数。The DataSet will not contain more than the number of records indicated by maxRecords. 但是,查询生成的整个结果集仍从服务器返回。However, the entire result set generated by the query is still returned from the server.

继承者说明

当在派生类中重写 Fill(DataSet) 时,请确保调用基类的 Fill(DataSet) 方法。When overriding Fill(DataSet) in a derived class, be sure to call the base class's Fill(DataSet) method.

另请参阅

Fill(Int32, Int32, DataTable[])

DataTable 中添加或刷新行,以与从指定的记录开始一直检索到指定的最大数目的记录的数据源中的行匹配。Adds or refreshes rows in a DataTable to match those in the data source starting at the specified record and retrieving up to the specified maximum number of records.

public:
 int Fill(int startRecord, int maxRecords, ... cli::array <System::Data::DataTable ^> ^ dataTables);
public int Fill (int startRecord, int maxRecords, params System.Data.DataTable[] dataTables);
override this.Fill : int * int * System.Data.DataTable[] -> int
Public Function Fill (startRecord As Integer, maxRecords As Integer, ParamArray dataTables As DataTable()) As Integer

参数

startRecord
Int32

从其开始的从零开始的记录号。The zero-based record number to start with.

maxRecords
Int32

要检索的最大记录数。The maximum number of records to retrieve.

dataTables
DataTable[]

要从数据源填充的 DataTable 对象。The DataTable objects to fill from the data source.

返回

已在 DataTable 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataTable. 此值不包括受不返回行的语句影响的行。This value does not include rows affected by statements that do not return rows.

注解

Fill 方法使用关联的 SelectCommand 属性指定的 SELECT 语句从数据源中检索行。The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. 与 SELECT 语句关联的连接对象必须有效,但不需要打开。The connection object associated with the SELECT statement must be valid, but it does not need to be open. 如果在调用 Fill 之前关闭连接,则会打开该连接以检索数据,然后将其关闭。If the connection is closed before Fill is called, it is opened to retrieve data, and then it is closed. 如果在调用 Fill 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before Fill is called, it remains open.

然后 Fill 操作将行添加到 DataSet中的目标 DataTable 对象,并创建 DataTable 对象(如果它们尚不存在)。The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist. 创建 DataTable 对象时,Fill 操作通常只创建列名称元数据。When creating DataTable objects, the Fill operation normally creates only column name metadata. 但是,如果 MissingSchemaAction 属性设置为 AddWithKey,则还会创建相应的主键和约束。However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

如果 SelectCommand 返回 OUTER JOIN 的结果,则 DataAdapter 不会为生成的 PrimaryKey 设置 DataTable值。If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. 必须显式定义主键,以确保正确解析重复行。You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

如果数据适配器在填充 DataTable时遇到重复的列,则使用 "columnname1"、"columnname2"、"columnname3" 等模式为后续列生成名称。If the data adapter 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.

当用于填充 DataSet 的 SELECT 语句返回多个结果(如批处理 SQL 语句)时,如果其中一个结果包含错误,则会跳过所有后续结果,并且不会将其添加到 DataSet中。When the SELECT statement used to populate the DataSet returns multiple results, such as a batch SQL statements, if one of the results contains an error, all subsequent results are skipped and not added to the DataSet.

您可以在同一 DataTable上多次使用 Fill 方法。You can use the Fill method multiple times on the same DataTable. 如果存在主键,则传入行与已存在的匹配行合并在一起。If a primary key exists, incoming rows are merged with matching rows that already exist. 如果主键不存在,将在 DataTable中追加传入行。If no primary key exists, incoming rows are appended to the 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.

另请参阅

Fill(DataTable)

DataSet 的指定范围中添加或刷新行,以与使用 DataTable 名称的数据源中的行匹配。Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataTable name.

public:
 int Fill(System::Data::DataTable ^ dataTable);
public int Fill (System.Data.DataTable dataTable);
override this.Fill : System.Data.DataTable -> int
Public Function Fill (dataTable As DataTable) As Integer

参数

dataTable
DataTable

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

返回

已在 DataSet 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataSet. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

例外

源表无效。The source table is invalid.

注解

Fill 方法使用关联的 SelectCommand 属性指定的 SELECT 语句从数据源中检索行。The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. 与 SELECT 语句关联的连接对象必须有效,但不需要打开。The connection object associated with the SELECT statement must be valid, but it does not need to be open. 如果在调用 Fill 之前关闭连接,则会打开该连接,以检索数据,然后关闭。If the connection is closed before Fill is called, it is opened to retrieve data, then closed. 如果在调用 Fill 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before Fill is called, it remains open.

然后 Fill 操作将行添加到 DataSet中的目标 DataTable 对象,并创建 DataTable 对象(如果它们尚不存在)。The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist. 创建 DataTable 对象时,Fill 操作通常只创建列名称元数据。When creating DataTable objects, the Fill operation normally creates only column name metadata. 但是,如果 MissingSchemaAction 属性设置为 AddWithKey,则还会创建相应的主键和约束。However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

如果 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.

采用 DataTable 作为参数的 Fill 的重载只获取第一个结果。The overload of Fill that takes DataTable as a parameter only obtains the first result. 使用 Fill 的重载,该重载采用 DataSet 作为参数以获取多个结果。Use an overload of Fill that takes DataSet as a parameter to obtain multiple results.

Fill 方法支持这样的方案: DataSet 包含多个 DataTable 对象,这些对象的名称仅区分大小写。The Fill method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 在这种情况下,Fill 执行区分大小写的比较来查找相应的表,如果不存在完全匹配项,则创建新表。In such situations, Fill 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.Fill(dataset, "aaa"); // Fills "aaa", which already exists in the DataSet.  
    adapter.Fill(dataset, "Aaa"); // Adds a new table called "Aaa".  

如果调用 FillDataSet 只包含一个名称只是大小写不同的 DataTable,则将更新该 DataTableIf Fill 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.Fill(dataset, "AAA"); // Fills table "aaa" because only one similarly named table is in the DataSet.  

您可以在同一 DataTable上多次使用 Fill 方法。You can use the Fill method multiple times on the same DataTable. 如果存在主键,则传入行与已存在的匹配行合并在一起。If a primary key exists, incoming rows are merged with matching rows that already exist. 如果主键不存在,将在 DataTable中追加传入行。If no primary key exists, incoming rows are appended to the DataTable.

如果 SelectCommand 返回 OUTER JOIN 的结果,则 DataAdapter 不会为生成的 PrimaryKey 设置 DataTable值。If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. 必须显式定义主键,以确保正确解析重复行。You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

备注

当处理返回多个结果的批处理 SQL 语句时,.NET Framework 数据提供程序的 FillFillSchema 的实现仅检索第一个结果的架构信息。When handling batch SQL statements that return multiple results, the implementation of Fill and FillSchema for a .NET Framework data provider retrieves schema information for only the first result.

继承者说明

Fill(DataSet) 方法的此重载受保护,旨在供 .NET Framework 数据提供程序使用。This overload of the Fill(DataSet) method is protected and is designed for use by a .NET Framework data provider.

另请参阅

Fill(DataTable, IDataReader)

DataTable 中添加或刷新行,以与使用指定的 DataTableIDataReader 名称的数据源中的行匹配。Adds or refreshes rows in a DataTable to match those in the data source using the specified DataTable and IDataReader names.

protected:
 virtual int Fill(System::Data::DataTable ^ dataTable, System::Data::IDataReader ^ dataReader);
protected virtual int Fill (System.Data.DataTable dataTable, System.Data.IDataReader dataReader);
override this.Fill : System.Data.DataTable * System.Data.IDataReader -> int
Protected Overridable Function Fill (dataTable As DataTable, dataReader As IDataReader) As Integer

参数

dataTable
DataTable

要用记录和架构(如果必要)填充的 DataTableA DataTable to fill with records and, if necessary, schema.

dataReader
IDataReader

IDataReader 的名称。The name of the IDataReader.

返回

已在 DataTable 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataTable. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

Fill(DataSet, String)

DataSet 中添加或刷新行以匹配使用 DataSetDataTable 名称的数据源中的行。Adds or refreshes rows in the DataSet to match those in the data source using the DataSet and DataTable names.

public:
 int Fill(System::Data::DataSet ^ dataSet, System::String ^ srcTable);
public int Fill (System.Data.DataSet dataSet, string srcTable);
override this.Fill : System.Data.DataSet * string -> int
Public Function Fill (dataSet As DataSet, srcTable As String) As Integer

参数

dataSet
DataSet

要用记录和架构(如果必要)填充的 DataSetA DataSet to fill with records and, if necessary, schema.

srcTable
String

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

返回

已在 DataSet 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataSet. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

例外

源表无效。The source table is invalid.

示例

下面的示例使用派生类 OleDbDataAdapter,使用 "类别" 表中的行填充 DataSetThe following example uses the derived class, OleDbDataAdapter, to fill a DataSet with rows from the categories table. 此示例假设已创建 OleDbDataAdapterDataSetThis example assumes that you have created an OleDbDataAdapter and a DataSet.

public void GetRecords()
{
    // ...
    // create dataSet and adapter
    // ...
    adapter.Fill(dataSet, "Categories");
}
Public Sub GetRecords()
    ' ...
    ' create dataSet and adapter
    ' ...
    adapter.Fill(dataSet, "Categories")
End Sub

注解

Fill 方法使用 SELECT 语句从数据源中检索数据。The Fill method retrieves the data from the data source using a SELECT statement. 与 select 命令关联的 IDbConnection 对象必须有效,但不需要将其打开。The IDbConnection object associated with the select command must be valid, but it does not need to be open. 如果 IDbConnectionFill 调用之前关闭,则将其打开以检索数据,然后关闭。If the IDbConnection is closed before Fill is called, it is opened to retrieve data, then closed. 如果在调用 Fill 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before Fill is called, it remains open.

如果命令未返回任何行,则不会将任何表添加到 DataSet中,也不会引发异常。If a command does not return any rows, no tables are added to the DataSet, and no exception is raised.

如果 DbDataAdapter 对象在填充 DataTable时遇到重复的列,它将使用模式 "columnname1"、"columnname2"、"columnname3" 等,为后续列生成名称。If the DbDataAdapter object encounters duplicate columns while populating a DataTable, it will generate 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.

当指定的查询返回多个结果时,每个结果集将放在一个单独的表中。When the query specified returns multiple results, 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). 由于没有为不返回行的查询创建表,因此,如果要处理一个 insert 查询后跟一个 select 查询,则为 select 查询创建的表将命名为 "Table",因为它是第一个创建的表。Since no table is created for a query that does not return rows, if you were to process an insert query followed by a select query, the table created for the select query would be named "Table", because it is the first table created. 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

Fill 方法支持这样的方案: DataSet 包含多个 DataTable 对象,这些对象的名称仅区分大小写。The Fill method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. 在这种情况下,Fill 执行区分大小写的比较来查找相应的表,如果不存在完全匹配项,则创建新表。In such situations, Fill 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.Fill(dataset, "aaa"); // Fills "aaa", which already exists in the DataSet.  
adapter.Fill(dataset, "Aaa"); // Adds a new table called "Aaa".  

如果调用 FillDataSet 只包含一个名称只是大小写不同的 DataTable,则将更新该 DataTableIf Fill 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.Fill(dataset, "AAA"); // Fills table "aaa" because only one similarly named table is in the DataSet.  

如果在填充数据表时出现错误或异常,则在出现错误之前添加的行将保留在数据表中。If an error or an exception is encountered while populating the data tables, rows added prior to the occurrence of the error remain in the data tables. 操作的剩余部分中止。The remainder of the operation is aborted.

当用于填充 DataSet 的 SELECT 语句返回多个结果(如批处理 SQL 语句)时,请注意以下事项:When the SELECT statement used to populate the DataSet returns multiple results, such as a batch SQL statement, be aware of the following:

  • 如果其中一个结果包含错误,则会跳过所有后续结果,并且不会将其添加到 DataSet中。If one of the results contains an error, all subsequent results are skipped and not added to the DataSet.

使用后续 Fill 调用来刷新 DataSet的内容时,必须满足两个条件:When using subsequent Fill calls to refresh the contents of the DataSet, two conditions must be met:

  1. SQL 语句应与最初用于填充 DataSet的语句匹配。The SQL statement should match the one initially used to populate the DataSet.

  2. 列信息必须存在。The Key column information must be present. 如果主键信息存在,则会对所有重复行进行协调,并且仅在对应于 DataSetDataTable 中出现一次。If primary key information is present, any duplicate rows are reconciled and only appear once in the DataTable that corresponds to the DataSet. 可以通过 FillSchema、指定 DataTablePrimaryKey 属性或将 MissingSchemaAction 属性设置为 AddWithKey来设置主键信息。Primary key information may be set either through FillSchema, by specifying the PrimaryKey property of the DataTable, or by setting the MissingSchemaAction property to AddWithKey.

如果 SelectCommand 返回 OUTER JOIN 的结果,则 DataAdapter 不会为生成的 PrimaryKey 设置 DataTable值。If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. 必须显式定义主键,以确保正确解析重复行。You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

备注

当处理返回多个结果的批处理 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.

继承者说明

当在派生类中重写 Fill(DataSet) 时,请确保调用基类的 Fill(DataSet) 方法。When overriding Fill(DataSet) in a derived class, be sure to call the base class's Fill(DataSet) method.

另请参阅

Fill(DataSet)

DataSet 中添加或刷新行。Adds or refreshes rows in the DataSet.

public:
 override int Fill(System::Data::DataSet ^ dataSet);
public override int Fill (System.Data.DataSet dataSet);
override this.Fill : System.Data.DataSet -> int
Public Overrides Function Fill (dataSet As DataSet) As Integer

参数

dataSet
DataSet

要用记录和架构(如果必要)填充的 DataSetA DataSet to fill with records and, if necessary, schema.

返回

已在 DataSet 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataSet. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

实现

注解

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

如果在填充数据表时出现错误或异常,则在出现错误之前添加的行将保留在数据表中。If an error or an exception is encountered while populating the data tables, rows added prior to the occurrence of the error remain in the data tables. 操作的剩余部分中止。The remainder of the operation is aborted.

如果命令未返回任何行,则不会将任何表添加到 DataSet中,也不会引发异常。If a command does not return any rows, no tables are added to the DataSet, and no exception is raised.

如果 DbDataAdapter 对象在填充 DataTable时遇到重复的列,则它会使用 "columnname1"、"columnname2"、"columnname3" 等模式为后续列生成名称。If the DbDataAdapter object 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.

当指定的查询返回多个结果时,返回查询的每个行的结果集将放在一个单独的表中。When the query specified returns multiple results, the result set for each row returning query 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). 由于没有为不返回行的查询创建表,因此,如果处理的是 insert 查询后跟 select 查询,则为 select 查询创建的表名为 "Table",因为它是第一个创建的表。Because no table is created for a query that does not return rows, if you process an insert query followed by a select query, the table created for the select query is named "Table" because it is the first table created. 使用列和表名称的应用程序应确保不会与这些命名模式发生冲突。Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

当用于填充 DataSet 的 SELECT 语句返回多个结果(如批处理 SQL 语句)时,如果其中一个结果包含错误,则会跳过所有后续结果,并且不会将其添加到 DataSet中。When the SELECT statement used to populate the DataSet returns multiple results, such as batch SQL statements, if one of the results contains an error, all subsequent results are skipped and are not added to the DataSet.

使用后续 Fill 调用来刷新 DataSet的内容时,必须满足两个条件:When using subsequent Fill calls to refresh the contents of the DataSet, two conditions must be met:

  1. SQL 语句应与最初用于填充 DataSet的语句匹配。The SQL statement should match the one initially used to populate the DataSet.

  2. 列信息必须存在。The Key column information must be present.

如果主键信息存在,则会对所有重复行进行协调,并且仅在对应于 DataSetDataTable 中出现一次。If primary key information is present, any duplicate rows are reconciled and only appear once in the DataTable that corresponds to the DataSet. 可以通过 FillSchema、指定 DataTablePrimaryKey 属性或将 MissingSchemaAction 属性设置为 AddWithKey来设置主键信息。Primary key information may be set either through FillSchema, by specifying the PrimaryKey property of the DataTable, or by setting the MissingSchemaAction property to AddWithKey.

如果 SelectCommand 返回 OUTER JOIN 的结果,则 DataAdapter 不会为生成的 PrimaryKey 设置 DataTable值。If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. 必须显式定义主键,以确保正确解析重复行。You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

备注

当处理返回多个结果的批处理 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.

另请参阅

Fill(DataTable, IDbCommand, CommandBehavior)

DataTable 中添加或刷新行,以与使用指定的 DataTableIDbCommandCommandBehavior 的数据源中的行匹配。Adds or refreshes rows in a DataTable to match those in the data source using the specified DataTable, IDbCommand and CommandBehavior.

protected:
 virtual int Fill(System::Data::DataTable ^ dataTable, System::Data::IDbCommand ^ command, System::Data::CommandBehavior behavior);
protected virtual int Fill (System.Data.DataTable dataTable, System.Data.IDbCommand command, System.Data.CommandBehavior behavior);
override this.Fill : System.Data.DataTable * System.Data.IDbCommand * System.Data.CommandBehavior -> int
Protected Overridable Function Fill (dataTable As DataTable, command As IDbCommand, behavior As CommandBehavior) As Integer

参数

dataTable
DataTable

要用记录和架构(如果必要)填充的 DataTableA DataTable to fill with records and, if necessary, schema.

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 中成功添加或刷新的行数。The number of rows successfully added to or refreshed in the DataTable. 这不包括受不返回行的语句影响的行。This does not include rows affected by statements that do not return rows.

注解

Fill 方法使用关联的 SelectCommand 属性指定的 SELECT 语句从数据源中检索行。The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. 与 SELECT 语句关联的连接对象必须有效,但不需要打开。The connection object associated with the SELECT statement must be valid, but it does not need to be open. 如果在调用 Fill 之前关闭连接,则会打开该连接,以检索数据,然后将其关闭。If the connection is closed before Fill is called, it is opened to retrieve data and then closed. 如果在调用 Fill 之前连接处于打开状态,则该连接将保持打开状态。If the connection is open before Fill is called, it remains open.

然后 Fill 操作将行添加到 DataSet中指定的目标 DataTable 对象中,如果该对象尚不存在,则创建 DataTable 该对象。The Fill operation then adds the rows to the specified destination DataTable object in the DataSet, creating the DataTable object if it does not already exist. 创建 DataTable 对象时,Fill 操作通常只创建列名称元数据。When creating a DataTable object, the Fill operation normally creates only column name metadata. 但是,如果 MissingSchemaAction 属性设置为 AddWithKey,则还会创建相应的主键和约束。However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

如果 DbDataAdapter 对象在填充 DataTable时遇到重复的列,则它将使用模式 "columnname1"、"columnname2"、"columnname3" 等为后续列生成名称。If the DbDataAdapter object encounters duplicate columns while populating a DataTable, it will generate 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.

采用 DataTable 作为参数的 Fill 的重载只获取第一个结果。The overload of Fill that takes DataTable as a parameter only obtains the first result. 使用 Fill 的重载,该重载采用 DataSet 作为参数以获取多个结果。Use an overload of Fill that takes DataSet as a parameter to obtain multiple results.

您可以在同一 DataTable上多次使用 Fill 方法。You can use the Fill method multiple times on the same DataTable. 如果存在主键,则传入行与已存在的匹配行合并在一起。If a primary key exists, incoming rows are merged with matching rows that already exist. 如果主键不存在,将在 DataTable中追加传入行。If no primary key exists, incoming rows are appended to the DataTable.

如果 SelectCommand 返回 OUTER JOIN 的结果,则 DataAdapter 不会为生成的 PrimaryKey 设置 DataTable值。If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. 必须显式定义主键,以确保正确解析重复行。You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. 有关详细信息,请参阅定义主键For more information, see Defining Primary Keys.

备注

当处理返回多个结果的批处理 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.

继承者说明

Fill(DataSet) 方法的此重载受保护,旨在供 .NET Framework 数据提供程序使用。This overload of the Fill(DataSet) method is protected and is designed for use by a .NET Framework data provider.

另请参阅

适用于