Fill method in SqlDataAdapter (2)

xiaoxiao2021-03-06  51

The last article we can see that the FillFromCommand and FillFromReader methods are called finally. We then investigate FillFromCommand that method private int FillFromCommand (object data, int startRecord, int maxRecords, string srcTable, IDbCommand command, CommandBehavior behavior) {IDbConnection connection1 = DbDataAdapter.GetConnection (command, "Fill"); // return connection ConnectionState state1 = ConnectionState.Open; if (missingschemaAction.addwithkey == base.missingschema)

// Add the necessary columns and primary key information to complete the architecture.

{Behavior | = CommandBehavior.KeyInfo;} int num1 = 0; try {try {DbDataAdapter.QuietOpen (connection1, out state1); // open the connection using (IDataReader reader1 = command.ExecuteReader (behavior | CommandBehavior.SequentialAccess)) // Read Data {IF (Data IS DataTable) {Num1 = this.Fill (DataTable) Data, Reader1); // This method we have

// I have said it finally calls FillFromreader (DataTable, Null, DataReader, 0, 0, NULL, NULL);

Else {Num1 = this.fill (DataSet) Data, Srctable, Reader1, StartRecord, MaxRecords;

// This method finally calls Return this.FillFromreader (DataSet, Srctable, DataReader,

// StartRecord, MaxRecords, Null, NULL;

}}} Finally {dbdataadapter.quietclose (connection1, state1); // Close connection}} Catch}}} Return Num1;} This is not difficult to see that fillFromMand is actually called FillFromReader, that is, all of the Fill method The operation will be completed by the FillFromreader method.

See FillFromReader Method: internal int FillFromReader (object data, string srcTable, IDataReader dataReader, int startRecord, int maxRecords, DataColumn parentChapterColumn, object parentChapterValue) {int num1 = 0; int num2 = 0; // schemaCountLabel_0004: if (0

// STARTRECORD starts MaxRecords record and returns record number} else {int Num3 = this.FillLoadDataRow (mapping1); // Fill data IF (1 == Num2) {Num1 = Num3;}}} finaryLy {mapping1. DataTable.EndLoadData (); // Open the notification, index maintenance and constraints after data loading. }}}}}}}}}} If (! This.FillNextResult (DataReader)) // Decision DataReader has recorded {Return Num1;} goto label_0004;} In fact, we have a general understanding of Fill, first Regardless of the Fill overload, it is finally called the FillFromReader method, and the structure map is created first in the FillFromReader, then populate the data. Then look at the structure of the structure, this is very complicated, please give me a lot of advice.

Structure mapping // private SchemaMapping FillSchemaMappingTry (object data, string srcTable, IDataReader dataReader, int schemaCount, DataColumn parentChapterColumn, object parentChapterValue) {SchemaMapping mapping1 = null; if (this.hasFillErrorHandler) {try {mapping1 = this.FillSchemaMapping (data, srcTable , dataReader, schemaCount, parentChapterColumn, parentChapterValue);} catch (Exception exception1) {this.FillErrorHandler (exception1, null, null);}} else {mapping1 = this.FillSchemaMapping (data, srcTable, dataReader, schemaCount, parentChapterColumn, parentChapterValue) ;} return mapping1;} // FillSchemaMappingprivate SchemaMapping FillSchemaMapping (object data, string srcTable, IDataReader dataReader, int schemaCount, DataColumn parentChapterColumn, object parentChapterValue) {SchemaMapping mapping1 = new SchemaMapping (this, dataReader, MissingschemaAction.addwithkey == base.missingsChemaAction); // Create a schemaMApping instance // Member name Description // MissingsChemaAction: // add Adds the necessary columns to complete the architecture. // AddWithkey adds the necessary column and primary key information to complete the architecture. See Fillschema for more information on how to add primary key information to DataTable. // Error generates InvalidOperationException if the specified column map is missing. // ignore ignores the extraranges.

string text1 = null; if (data is DataTable) {mapping1.DataTable = (DataTable) data;} else {mapping1.DataSet = (DataSet) data; text1 = DbDataAdapter.GetSourceTableName (srcTable, schemaCount); // DataSet to be created Name of the table / ** * Internal static string getSourceTablename (string srctable, int index) * {* ifx == 0) * {* return srctable; *} * Return (srctable index.toString ()); *} ** /} mapping1.setupschema (Schematype.mapped, Text1, True, ParentchapterChaptercolumn, Parentchapter,); // / * * * public enum scheMATYPE * {* // fields * mapped = 2, apply any existing table maps to Introducing the architecture, configuring the DataSet with the conversion architecture. * Source = 1 Ignore any table mapping on the DataAdapter. Use the incoming architecture to configure DataSet without any conversion. *} ** /

return mapping1;} // SchemaMappinginternal SchemaMapping (DbDataAdapter adapter, IDataReader dataReader, bool keyInfo) {this.adapter = adapter; this.dataReader = dataReader; if (keyInfo) // if the primary key columns and must be added, use of the GetSchemaTable dataReader ( Returns a DataTable {this.schematable = DataReader.getschematable (); // Returns a DataTable, which describes the column data of SqlDataReader. Can view MSDN

}} // SetupSchema create internal void SetupSchema (SchemaType schemaType, string sourceTableName, bool gettingData, DataColumn parentChapterColumn, object parentChapterValue) {MissingMappingAction action1; MissingSchemaAction action2; if (SchemaType.Mapped == schemaType) {action1 = this.adapter.MissingMappingAction; / / Determine the incoming data that does not match the table or the action that needs to be performed acts .., this.adapter.MissingSChemaAction; // Determines the operation that the existing DataSet architecture needs to be executed when the data does not match. If (! Adp.isempty (sourceable)) {this.tablemapping = this.adapter.gettableMappingByschemaAction (SourceTableName, SourceTableName, Action1); // Create the main mapping between the source table and the DATATABLE. goto Label_016D;} if (this.dataTable == null) {goto Label_016D;} int num1 = this.adapter.IndexOfDataSetTable (this.dataTable.TableName); if (! = num1 -1) {this.tableMapping = this.adapter .TableMappings [Num1]; Goto Label_016d;} Switch (Action1) {CASE MISSINGMAPPINGAPINGATION.PASSTHROUGH: // Create a source or source table and add it to DataSet using its original name. {This.tablemapping = new datatablemapping (this.dataable.tablename); goto label_016d;} Case MissingMappingAction.Ignore: // Ignore the column or table without mapping.

{This.tableMapping = null; goto Label_016D;} case MissingMappingAction.Error: // missing the specified column mapping, generating InvalidOperationException {throw ADP.MissingTableMappingDestination (this.dataTable.TableName);}} throw ADP.InvalidMappingAction ((int) action1);} if (SchemaType.Source = schemaType) {throw ADP.InvalidSchemaType ((int!) schemaType);} action1 = MissingMappingAction.Passthrough; action2 = MissingSchemaAction.Add; if (! ADP.IsEmpty (sourceTableName)) {this .tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (null, sourceTableName, sourceTableName, action1);} else if (! this.dataTable = null) {int num2 = this.adapter.IndexOfDataSetTable (this.dataTable.TableName); if (-1 =! Num2) {this.tablemapp ING = this.adapter.tableMappings [Num2];} else {this.tablemapping = new datatablemapping (this.datatable.tablename, this.dataable.tablename); Create a master map between the source table and DataTable. }} Label_016D: if (! This.tableMapping = null) {if (this.dataTable == null) {this.dataTable = this.tableMapping.GetDataTableBySchemaAction (this.dataSet, action2); action2 acquired using the value of the current DataSet DataTable .

You can view msdn if (this.dataTable == null) {return;}} if (this.schemaTable == null) {this.SetupSchemaWithoutKeyInfo (action1, action2, gettingData, parentChapterColumn, parentChapterValue);} else {this.SetupSchemaWithKeyInfo (action1 , action2, gettingData, parentChapterColumn, parentChapterValue);}}} // FillLoadDataRowChunkprivate int FillLoadDataRowChunk (SchemaMapping mapping, int startRecord, int maxRecords) {IDataReader reader1 = mapping.DataReader; while (0 = maxRecords) {goto label_0062;} BOOL FLAG1 = BASE.ACCEPTCHANGESDURINGFILL; / / During the Fill operation, do accepting AcceptChanges call it for DataROW .

转载请注明原文地址:https://www.9cbs.com/read-116165.html

New Post(0)