Assembly : System.Data (dans system.data.dll)
SyntaxePublic Overridable Sub Load ( _ reader As IDataReader, _ loadOption As LoadOption, _ errorHandler As FillErrorEventHandler, _ ParamArray tables As DataTable() _ )
Dim instance As DataSet Dim reader As IDataReader Dim loadOption As LoadOption Dim errorHandler As FillErrorEventHandler Dim tables As DataTable() instance.Load(reader, loadOption, errorHandler, tables)
public virtual void Load ( IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler, params DataTable[] tables )
public: virtual void Load ( IDataReader^ reader, LoadOption loadOption, FillErrorEventHandler^ errorHandler, ... array<DataTable^>^ tables )
public void Load ( IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler, DataTable[] tables )
public function Load ( reader : IDataReader, loadOption : LoadOption, errorHandler : FillErrorEventHandler, ... tables : DataTable[] )
Paramètres
- reader
IDataReader qui fournit un ou plusieurs jeux de résultats.
- loadOption
Valeur issue de l'énumération LoadOption qui indique comment les lignes déjà présentes dans les instances de DataTable du DataSet seront associées aux lignes entrantes qui partagent la même clé primaire.
- errorHandler
Un délégué FillErrorEventHandler à appeler lorsqu'une erreur se produit en chargeant des données.
- tables
Tableau d'instances de DataTable à partir desquelles la méthode Load récupère les informations de nom et d'espace de noms.
NotesLa méthode Load fournit une technique permettant de remplir un DataTable unique avec des données, récupérées à partir d'une instance de IDataReader. Cette méthode fournit les mêmes fonctionnalités, mais elle vous permet de charger plusieurs jeux de résultats à partir d'un IDataReader dans plusieurs tables d'un DataSet.
Le paramètre loadOption vous permet de spécifier comment les données importées doivent interagir avec les données existantes et peuvent être des valeurs quelconques de l'énumération LoadOption. Consultez la documentation sur la méthode DataTableLoad pour plus d'informations sur l'utilisation de ce paramètre.
Le paramètre errorHandler est un délégué FillErrorEventHandler qui fait référence à une procédure qui est appelée lorsqu'une erreur se produit en chargeant des données. Le paramètre FillErrorEventArgs passé à la procédure fournit des propriétés qui vous permettent de récupérer des informations sur l'erreur qui s'est produite, la ligne actuelle de données et le DataTable en cours de remplissage. L'utilisation de ce mécanisme de délégué à la place d'un block try/catch plus simple vous permet de déterminer l'erreur, de gérer la situation et de continuer le processus si vous le souhaitez. Le paramètre FillErrorEventArgs fournit une propriété Continue : affectez la valeur true à cette propriété pour indiquer que vous avez géré l'erreur et souhaitez continuer le processus ; affectez la valeur false à la propriété pour indiquer que vous souhaitez interrompre le processus. Sachez qu'affecter la valeur false à la propriété entraîne la levée d'une exception par le code qui a déclenché le problème.
Le paramètre tables vous permet de spécifier un tableau d'instances de DataTable en indiquant l'ordre des tables correspondant aux différents jeux de résultats chargés à partir du lecteur. La méthode Load remplit chaque instance de DataTable fournie avec les données d'un jeu de résultats unique provenant du lecteur des données sources. Après chaque jeu de résultats, la méthode Load passe au jeu de résultats suivant dans le lecteur, jusqu'au dernier jeu de résultats.
Le schéma de résolution de noms de cette méthode est le même que celui suivi par la méthode Fill de la classe DbDataAdapter.
ExempleL'exemple suivant ajoute une table à un DataSet, puis essaie d'utiliser la méthode Load pour charger des données à partir d'un DataTableReader qui contient un schéma incompatible. Plutôt qu'intercepter l'erreur, cet exemple utilise un délégué FillErrorEventHandler pour étudier et gérer l'erreur.
Sub Main() Dim dataSet As New DataSet Dim table As New DataTable() ' Attempt to load data from a data reader in which ' the schema is incompatible with the current schema. ' If you use exception handling, you won't get the chance ' to examine each row, and each individual table, ' as the Load method progresses. ' By taking advantage of the FillErrorEventHandler delegate, ' you can interact with the Load process as an error occurs, ' attempting to fix the problem, or simply continuing or quitting ' the Load process.: dataSet = New DataSet() table = GetIntegerTable() dataSet.Tables.Add(table) Dim reader As New DataTableReader(GetStringTable()) dataSet.Load(reader, LoadOption.OverwriteChanges, _ AddressOf FillErrorHandler, table) Console.WriteLine("Press any key to continue.") Console.ReadKey() End Sub Private Sub FillErrorHandler(ByVal sender As Object, _ ByVal e As FillErrorEventArgs) ' You can use the e.Errors value to determine exactly what ' went wrong. If e.Errors.GetType Is GetType(System.FormatException) Then Console.WriteLine("Error when attempting to update the value: {0}", _ e.Values(0)) End If ' Setting e.Continue to True tells the Load ' method to continue trying. Setting it to False ' indicates that an error has occurred, and the ' Load method raises the exception that got ' you here. e.Continue = True End Sub Private Function GetIntegerTable() As DataTable ' Create sample table with a single Int32 column. Dim table As New DataTable Dim idColumn As DataColumn = table.Columns.Add("ID", _ GetType(Integer)) ' Set the ID column as the primary key column. table.PrimaryKey = New DataColumn() {idColumn} table.Rows.Add(New Object() {4}) table.Rows.Add(New Object() {5}) table.TableName = "IntegerTable" table.AcceptChanges() Return table End Function Private Function GetStringTable() As DataTable ' Create sample table with a single String column. Dim table As New DataTable Dim idColumn As DataColumn = table.Columns.Add("ID", _ GetType(String)) ' Set the ID column as the primary key column. table.PrimaryKey = New DataColumn() {idColumn} table.Rows.Add(New Object() {"Mary"}) table.Rows.Add(New Object() {"Andy"}) table.Rows.Add(New Object() {"Peter"}) table.AcceptChanges() Return table End Function Private Sub PrintColumns( _ ByVal table As DataTable) ' Loop through all the rows in the DataTableReader. For Each row As DataRow In table.Rows For Each col As DataColumn In table.Columns Console.Write(row(col).ToString() & " ") Next Console.WriteLine() Next End Sub
static void Main() { // Attempt to load data from a data reader in which // the schema is incompatible with the current schema. // If you use exception handling, you won't get the chance // to examine each row, and each individual table, // as the Load method progresses. // By taking advantage of the FillErrorEventHandler delegate, // you can interact with the Load process as an error occurs, // attempting to fix the problem, or simply continuing or quitting // the Load process.: DataSet dataSet = new DataSet(); DataTable table = GetIntegerTable(); dataSet.Tables.Add(table); DataTableReader reader = new DataTableReader(GetStringTable()); dataSet.Load(reader, LoadOption.OverwriteChanges, FillErrorHandler, table); Console.WriteLine("Press any key to continue."); Console.ReadKey(); } private static DataTable GetIntegerTable() { // Create sample Customers table, in order // to demonstrate the behavior of the DataTableReader. DataTable table = new DataTable(); // Create two columns, ID and Name. DataColumn idColumn = table.Columns.Add("ID", typeof(int)); // Set the ID column as the primary key column. table.PrimaryKey = new DataColumn[] { idColumn }; table.Rows.Add(new object[] { 4 }); table.Rows.Add(new object[] { 5 }); table.AcceptChanges(); return table; } private static DataTable GetStringTable() { // Create sample Customers table, in order // to demonstrate the behavior of the DataTableReader. DataTable table = new DataTable(); // Create two columns, ID and Name. DataColumn idColumn = table.Columns.Add("ID", typeof(string)); // Set the ID column as the primary key column. table.PrimaryKey = new DataColumn[] { idColumn }; table.Rows.Add(new object[] { "Mary" }); table.Rows.Add(new object[] { "Andy" }); table.Rows.Add(new object[] { "Peter" }); table.AcceptChanges(); return table; } static void FillErrorHandler(object sender, FillErrorEventArgs e) { // You can use the e.Errors value to determine exactly what // went wrong. if (e.Errors.GetType() == typeof(System.FormatException)) { Console.WriteLine("Error when attempting to update the value: {0}", e.Values[0]); } // Setting e.Continue to True tells the Load // method to continue trying. Setting it to False // indicates that an error has occurred, and the // Load method raises the exception that got // you here. e.Continue = true; }
L'exemple affiche la sortie suivante dans la fenêtre de la console :
Error when attempting to update the value: Mary Error when attempting to update the value: Andy Error when attempting to update the value: Peter
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ