IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Probl�me datatable.Load() sur un IDataReader custom


Sujet :

C#

  1. #1
    Expert confirm�
    Avatar de StringBuilder
    Homme Profil pro
    Chef de projets
    Inscrit en
    F�vrier 2010
    Messages
    4 197
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 46
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations professionnelles :
    Activit� : Chef de projets
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : F�vrier 2010
    Messages : 4 197
    Billets dans le blog
    1
    Par d�faut Probl�me datatable.Load() sur un IDataReader custom
    Bonjour,

    Pour faire suite � mon message pr�c�dent � propos d'une impl�mentation custo de IDbConnection, j'en suis maintenant � IDataReader.

    Si une �me charitable souhaite se pencher dessus, voici le code complet :

    XmlConnection.cs
    Code csharp : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    using System.Data;
    using System.Diagnostics.CodeAnalysis;
    using System.Xml;
     
    namespace AZToolBox.Xml
    {
        public class XmlConnection : IDbConnection
        {
            private bool open = false;
            internal XmlReader? Reader;
     
            private bool disposedValue;
     
            public XmlConnection() : this(null) 
            {
            }
     
            public XmlConnection(string? connectionString)
            {
                ConnectionString = connectionString;
            }
     
            [AllowNull]
            public string ConnectionString { get; set; }
     
            /// <summary>
            /// Not used
            /// </summary>
            public int ConnectionTimeout { get; set; }
     
            public string Database
            {
                get
                {
                    if (Reader is not null && State == ConnectionState.Open)
                    {
                        return Reader.BaseURI;
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
            }
     
            public ConnectionState State
            {
                get
                {
                    if (open)
                    {
                        return ConnectionState.Open;
                    }
                    else
                    {
                        return ConnectionState.Closed;
                    }
                }
            }
     
            public IDbTransaction BeginTransaction()
            {
                throw new NotImplementedException();
            }
     
            public IDbTransaction BeginTransaction(IsolationLevel il)
            {
                throw new NotImplementedException();
            }
     
            public void ChangeDatabase(string databaseName)
            {
                throw new NotImplementedException();
            }
     
            public void Close()
            {
                if (Reader is not null)
                {
                    Reader.Close();
                    Reader.Dispose();
                    Reader = null;
                }
                open = false;
            }
     
            public XmlCommand CreateCommand()
            {
                return new XmlCommand(this);
            }
     
            IDbCommand IDbConnection.CreateCommand() => CreateCommand();
     
            public void Open()
            {
                if (ConnectionString is null)
                {
                    throw new Exception("Pas de chaîne de connexion spécifiée !");
                }
                Reader = XmlReader.Create(ConnectionString);
                open = true;
            }
     
            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: supprimer l'état managé (objets managés)
                        Close();
                    }
     
                    // TODO: libérer les ressources non managées (objets non managés) et substituer le finaliseur
                    // TODO: affecter aux grands champs une valeur null
                    disposedValue = true;
                }
            }
     
            // // TODO: substituer le finaliseur uniquement si 'Dispose(bool disposing)' a du code pour libérer les ressources non managées
            // ~XmlConnection()
            // {
            //     // Ne changez pas ce code. Placez le code de nettoyage dans la méthode 'Dispose(bool disposing)'
            //     Dispose(disposing: false);
            // }
     
            public void Dispose()
            {
                // Ne changez pas ce code. Placez le code de nettoyage dans la méthode 'Dispose(bool disposing)'
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    }

    XmlCommand.cs
    Code csharp : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    using System.Data;
    using System.Diagnostics.CodeAnalysis;
    using System.Xml.XPath;
     
    namespace AZToolBox.Xml
    {
        public class XmlCommand : IDbCommand
        {
            private XmlConnection _connection;
            private CommandType _commandType;
            private bool disposedValue;
            private string _query = string.Empty;
            private List<ColumnDefinition> _columns = [];
     
            internal XmlCommand(XmlConnection connection)
            {
                _connection = connection;
                _commandType = CommandType.Text;
            }
     
            [AllowNull]
            public string CommandText
            {
                get
                {
                    return $"select {string.Join(',', _columns)} from {_query}";
                }
                set
                {
                    _query = string.Empty;
                    _columns = [];
                    if (value is null)
                    {
                        throw new ArgumentNullException("value");
                    }
                    int s = value.IndexOf("select ");
                    int f = value.IndexOf(" from ");
                    if (s != -1 && f != -1)
                    {
                        string columns = value.Substring(s + "select ".Length, f - (s + "select ".Length));
                        _columns.AddRange(columns.Split([',']).Select(c => (ColumnDefinition)c));
                        _query = value.Substring(f + " from ".Length);
                    }
                    else if (value.Length > 0)
                    {
                        throw new InvalidExpressionException(value);
                    }
                }
            }
     
            public int CommandTimeout { get; set; }
     
            public CommandType CommandType
            {
                get
                {
                    return _commandType;
                }
                set
                {
                    if (value != CommandType.Text)
                    {
                        throw new NotImplementedException(value.ToString());
                    }
                    else
                    {
                        _commandType = value;
                    }
                }
            }
     
            public IDbConnection? Connection
            {
                get
                {
                    return _connection;
                }
                set
                {
                    XmlConnection? connection = value as XmlConnection;
                    if (connection is not null)
                    {
                        _connection = connection;
                    }
                    else
                    {
                        throw new ArgumentNullException(nameof(value));
                    }
                }
            }
     
            public IDataParameterCollection Parameters => throw new NotImplementedException();
     
            public IDbTransaction? Transaction { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
     
            public UpdateRowSource UpdatedRowSource { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
     
            public void Cancel()
            {
                throw new NotImplementedException();
            }
     
            public IDbDataParameter CreateParameter()
            {
                throw new NotImplementedException();
            }
     
            public int ExecuteNonQuery()
            {
                throw new NotImplementedException();
            }
     
            public XmlDataReader ExecuteReader() => ExecuteReader(CommandBehavior.Default);
     
            public XmlDataReader ExecuteReader(CommandBehavior behavior)
            {
                if (_connection.State != ConnectionState.Open)
                {
                    throw new Exception("La connection doit être ouverte !");
                }
                XPathDocument _document = new XPathDocument(_connection.Reader!);
                XPathNavigator _navigator = _document.CreateNavigator();
                XPathNodeIterator _iterator = _navigator.Select(_query);
                return new XmlDataReader(_iterator, _columns);
            }
     
            IDataReader IDbCommand.ExecuteReader() => ExecuteReader(CommandBehavior.Default);
     
            IDataReader IDbCommand.ExecuteReader(CommandBehavior behavior) => ExecuteReader(behavior);
     
     
            public object? ExecuteScalar()
            {
                XmlDataReader _reader = (ExecuteReader() as XmlDataReader)!;
                _reader.Read();
                object? res = _reader.GetValue(0);
                _reader.Close();
                return res;
            }
     
            /// <summary>
            /// Does nothing
            /// </summary>
            public void Prepare()
            {
                // Do nothing
            }
     
            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: supprimer l'état managé (objets managés)
                    }
     
                    // TODO: libérer les ressources non managées (objets non managés) et substituer le finaliseur
                    // TODO: affecter aux grands champs une valeur null
                    disposedValue = true;
                }
            }
     
            // // TODO: substituer le finaliseur uniquement si 'Dispose(bool disposing)' a du code pour libérer les ressources non managées
            // ~XmlCommand()
            // {
            //     // Ne changez pas ce code. Placez le code de nettoyage dans la méthode 'Dispose(bool disposing)'
            //     Dispose(disposing: false);
            // }
     
            public void Dispose()
            {
                // Ne changez pas ce code. Placez le code de nettoyage dans la méthode 'Dispose(bool disposing)'
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
     
        internal class ColumnDefinition
        {
            public string Name { get; set; }
            public string Value { get; set; }
     
            public ColumnDefinition(string definition)
            {
                int a = definition.IndexOf(" as ");
                if (a != -1)
                {
                    Value = definition.Substring(0, a).Trim();
                    Name = definition.Substring(a + " as ".Length).Trim();
                }
                else
                {
                    Value = definition.Trim();
                    Name = Value;
                }
            }
     
            public override string ToString()
            {
                return $"{Value} as {Name}";
            }
     
            public static implicit operator string(ColumnDefinition c)
            {
                return c.ToString();
            }
     
            public static implicit operator ColumnDefinition(string c)
            {
                return new ColumnDefinition(c);
            }
        }
    }

    Et enfin XmlDataReader.cs
    Code csharp : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Diagnostics.CodeAnalysis;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.XPath;
     
    namespace AZToolBox.Xml
    {
        public class XmlDataReader : IDataReader
        {
            private bool disposedValue;
            private bool closed = true;
     
            private XPathNodeIterator _iterator;
            private List<ColumnDefinition> _columns;
     
            internal XmlDataReader(XPathNodeIterator iterator, List<ColumnDefinition> columns)
            {
                _iterator = iterator;
                _columns = columns;
                closed = false;
            }
     
            public object this[int i] => GetValue(i);
     
            public object this[string name] => GetValue(GetOrdinal(name));
     
            /// <summary>
            /// Always zero
            /// </summary>
            public int Depth => 0;
     
            public bool IsClosed => closed;
     
            /// <summary>
            /// Always zero
            /// </summary>
            public int RecordsAffected => 0;
     
            public int FieldCount => _columns.Count;
     
            public void Close() => closed = true;
     
            public bool GetBoolean(int i) => bool.Parse(GetString(i));
     
            public byte GetByte(int i) => byte.Parse(GetString(i));
     
            public long GetBytes(int i, long fieldOffset, byte[]? buffer, int bufferoffset, int length)
            {
                int l = -1;
                if (buffer is not null)
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(GetString(i), (int)fieldOffset, length);
                    bytes.CopyTo(buffer!, bufferoffset);
                    l = bytes.Length;
                }
                return l;
            }
     
            public char GetChar(int i)
            {
                string s = GetString(i);
                if (s.Length > 1)
                {
                    throw new InvalidCastException();
                }
                else
                {
                    return GetString(i)[0];
                }
            }
     
            public long GetChars(int i, long fieldoffset, char[]? buffer, int bufferoffset, int length)
            {
                int l = -1;
                if (buffer is not null)
                {
                    char[] chars = GetString(i).ToCharArray((int)fieldoffset, length);
                    chars.CopyTo(buffer!, bufferoffset);
                    l = chars.Length;
                }
                return l;
            }
     
            public IDataReader GetData(int i)
            {
                throw new NotImplementedException();
            }
     
            public string GetDataTypeName(int i) => DataType.Text.ToString();
     
            public DateTime GetDateTime(int i) => DateTime.Parse(GetString(i));
     
            public decimal GetDecimal(int i) => decimal.Parse(GetString(i));
     
            public double GetDouble(int i) => double.Parse(GetString(i));
     
            [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)]
            public Type GetFieldType(int i) => typeof(string);
     
            public float GetFloat(int i) => float.Parse(GetString(i));
     
            public Guid GetGuid(int i) => Guid.Parse(GetString(i));
     
            public short GetInt16(int i) => short.Parse(GetString(i));
     
            public int GetInt32(int i) => int.Parse(GetString(i));
     
            public long GetInt64(int i) => long.Parse(GetString(i));
     
            public string GetName(int i) => _columns[i].Name;
     
            public int GetOrdinal(string name) => _columns.FindIndex(a => a.Name == name);
     
            public DataTable? GetSchemaTable()
            {
                return null;
                /*
                DataTable schemaTable = new();
                schemaTable.Columns.Add("ColumnName", typeof(string));
                schemaTable.Columns.Add("ColumnOrdinal", typeof(int));
                schemaTable.Columns.Add("DataType", typeof(Type));
                schemaTable.Columns.Add("IsReadOnly", typeof(bool));
                
     
                for (int i = 0; i < FieldCount; i++)
                {
                    DataRow row = schemaTable.NewRow();
                    row["ColumnName"] = GetName(i);
                    row["ColumnOrdinal"] = i;
                    row["DataType"] = GetFieldType(i);
                    row["IsReadOnly"] = true;
                    schemaTable.Rows.Add(row);
                }
                return schemaTable;
                */
            }
     
            public string GetString(int i)
            {
                XPathNavigator? nav = _iterator.Current!.SelectSingleNode(_columns[i].Value);
                if (nav is null)
                {
                    return "null";
                }
                else if (nav.IsEmptyElement)
                {
                    return string.Empty;
                }
                else if (nav.IsNode)
                {
                    return nav.Value;
                }
                throw new NotImplementedException();
            }
     
            public object GetValue(int i)
            {
                string s = GetString(i);
                if (s == "null")
                {
                    return DBNull.Value;
                }
                else
                {
                    return s;
                }
            }
     
            public int GetValues(object[] values)
            {
                int read = 0;
                for (int i = 0; i < _columns.Count && i < values.Length; i++)
                {
                    values[i] = GetValue(i);
                    read++;
                }
                return read;
            }
     
            public bool IsDBNull(int i) => GetString(i) == "null";
     
            public bool NextResult() => false;
     
            public bool Read() => _iterator.MoveNext();
     
            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: supprimer l'état managé (objets managés)
                    }
     
                    // TODO: libérer les ressources non managées (objets non managés) et substituer le finaliseur
                    // TODO: affecter aux grands champs une valeur null
                    disposedValue = true;
                }
            }
     
            // // TODO: substituer le finaliseur uniquement si 'Dispose(bool disposing)' a du code pour libérer les ressources non managées
            // ~XmlDataReader()
            // {
            //     // Ne changez pas ce code. Placez le code de nettoyage dans la méthode 'Dispose(bool disposing)'
            //     Dispose(disposing: false);
            // }
     
            public void Dispose()
            {
                // Ne changez pas ce code. Placez le code de nettoyage dans la méthode 'Dispose(bool disposing)'
                Dispose(disposing: true);
                GC.SuppressFinalize(this);
            }
        }
    }

    Le code tel quel fonctionne plut�t bien.
    Il y a plein de choses qui pourraient �tre am�lior�es, notamment la gestion des types en se basant sur un XSD, etc.

    Ce qui me pose probl�me ici, c'est que lorsque je tente d'utiliser mon code :
    Form1.cs
    Code csharp : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    using AZToolBox.Xml;
    using System.Data;
     
    namespace WinFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
     
                using (XmlConnection cnx = new("test.xml"))
                {
                    cnx.Open();
                    XmlCommand cmd = cnx.CreateCommand();
                    cmd.CommandText = "select @firstname as Nom, @lastname as Prenom, @birth as Age from /personnes/personne[substring(@birth, 1, 4) > '2024' or (substring(@birth, 1, 4) = '2024' and substring(@birth, 6, 2) > '03')]";
                    XmlDataReader xmlDataReader = cmd.ExecuteReader();
     
                    DataTable dataTable = new();
                    dataTable.Load(xmlDataReader);
     
                    dataGridView1.DataSource = dataTable;
                    cnx.Close();
                }
            }
        }
    }

    Le dataTable.Load(xmlDataReader); appelle XmlDataReader.GetSchemaTable() dont le code est en commentaire (remplac� par un return null) ci-dessus, XmlDataReader.cs ligne 121 et suivantes.

    Le code en commentaire est celui que m'a g�n�r� Copilot, et que j'ai tent� de compl�ter avec ce que j'ai trouv� dans la documentation de Systel.Data.SqlClient.GetSchemaTable()
    https://learn.microsoft.com/en-us/do...ckFrom=net-9.0

    Pourtant si je r�active le code de la m�thode, je me retrouve avec l'erreur suivante :
    Nom : Capture d��cran 2025-01-24 175423.png
Affichages : 164
Taille : 26,8 Ko

    Je n'arrive pas � obtenir le code de la fonction DataTable.Load() pour avoir plus de d�tails sur l'origine de l'erreur.
    Dans mon code que j'ai aucun argument qui s'appelle Arg_ParamName_Name ou column.

    J'aimerais pourtant avoir un schema propre, ce qui me permettra dans le future d'avoir une meilleure gestion des types (actuellement tout est g�r� en string, ce qui fait le job avec du XML...)

    -- Edit : ah, oui, et un exemple de fichier xml

    test.xml
    Code xml : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?xml version="1.0" encoding="utf-8" ?>
    <personnes>
      <personne firstname="Toto1" lastname="Titi1" birth="2024-01-01"/>
      <personne firstname="Toto2" lastname="Titi2" birth="2024-02-01"/>
      <personne firstname="Toto3" lastname="Titi3" birth="2024-03-01"/>
      <personne firstname="Toto3" lastname="Titi4" birth="2024-04-01"/>
      <personne firstname="Toto4" lastname="Titi5" birth="2024-05-01"/>
      <personne firstname="Toto5" lastname="Titi6" birth="2024-06-01"/>
      <personne firstname="Toto6" lastname="Titi7" birth="2024-07-01"/>
      <personne firstname="Toto7" lastname="Titi8" birth="2024-08-01"/>
    </personnes>

  2. #2
    Membre Expert
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    1 592
    D�tails du profil
    Informations personnelles :
    �ge : 46
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 1 592
    Par d�faut
    A priori je pense que l'erreur voudrait dire que la requ�te en renvoi rien, donc xmlDataReader serait null

  3. #3
    Expert confirm�
    Avatar de StringBuilder
    Homme Profil pro
    Chef de projets
    Inscrit en
    F�vrier 2010
    Messages
    4 197
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 46
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations professionnelles :
    Activit� : Chef de projets
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : F�vrier 2010
    Messages : 4 197
    Billets dans le blog
    1
    Par d�faut
    Non, pas de souci � ce niveau.
    La preuve, si GetTableSchema() du XmlDataReader retourne null, aucun souci pour charger le DataTable et le DataGridView ensuite.

  4. #4
    Membre Expert
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    700
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 700
    Par d�faut
    Bonsoir,
    bizarre avec un projet neuf les 3 classes copi�es-coll�es j'ai le r�sultat escompt� :

    Nom : 2025-01-24_202919.jpg
Affichages : 140
Taille : 46,8 Ko

  5. #5
    Expert confirm�
    Avatar de StringBuilder
    Homme Profil pro
    Chef de projets
    Inscrit en
    F�vrier 2010
    Messages
    4 197
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 46
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations professionnelles :
    Activit� : Chef de projets
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : F�vrier 2010
    Messages : 4 197
    Billets dans le blog
    1
    Par d�faut
    Ligne 121 du XmlDataReader.cs
    Retire le return null; et d�commente les lignes suivantes.

    Tu devrais avoir l'erreur.

    Pourtant c'est pas l� que �a plante. C'est quelque part dans le .Load() qu'il fait un truc avec le sch�ma, mais pas moyen de comprendre ce qui ne lui va pas...

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    700
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 700
    Par d�faut
    Bonsoir,
    oui, j'avais lu un peu trop vite !!!

    Et en �crivant :
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
     DataTable dtSchema = xmlDataReader.GetSchemaTable()!;
    source

    VS m'a �crit la suite :
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    using AZToolBox.Xml;
    using System.Data;
    using System.Xml;
     
    namespace IDbConnection_et_nullablité
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
     
                using (XmlConnection cnx = new("test.xml"))
                {
                    cnx.Open();
                    XmlCommand cmd = cnx.CreateCommand();
                    cmd.CommandText = "select @firstname as Nom, @lastname as Prenom, @birth as Age from /personnes/personne[substring(@birth, 1, 4) > '2024' or (substring(@birth, 1, 4) = '2024' and substring(@birth, 6, 2) > '03')]";
                    XmlDataReader xmlDataReader = cmd.ExecuteReader();
     
                    DataTable dtSchema = xmlDataReader.GetSchemaTable()!;
                    DataTable dt = new DataTable();
                    foreach (DataRow row in dtSchema.Rows)
                    {
                        DataColumn column = new DataColumn(row["ColumnName"].ToString(), (Type)row["DataType"]);
                        dt.Columns.Add(column);
                    }
     
                    while (xmlDataReader.Read())
                    {
                        DataRow row = dt.NewRow();
                        for (int i = 0; i < xmlDataReader.FieldCount; i++)
                        {
                            row[i] = xmlDataReader.GetValue(i);
                        }
                        dt.Rows.Add(row);
                    }
     
                    dataGridView1.DataSource = dt;
     
     
                    cnx.Close();
                }
            }
        }
    }
    Cela semble fonctionner, mais j'ai test� tr�s rapidement...

    Optimis� avec "Load" :
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
                    using (XmlDataReader xmlDataReader = cmd.ExecuteReader())
                    {
                        DataTable dtSchema = xmlDataReader.GetSchemaTable()!;
                        DataTable dt = new DataTable();
     
                        foreach (DataRow row in dtSchema.Rows)
                        {
                            DataColumn column = new DataColumn(row["ColumnName"].ToString(), (Type)row["DataType"]);
                            dt.Columns.Add(column);
                        }
     
                        dt.Load(xmlDataReader);
     
                        dataGridView1.DataSource = dt;
                    }
     
                    cnx.Close();

  7. #7
    Membre Expert
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    700
    D�tails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 700
    Par d�faut
    Bonjour,
    Si on n�impose pas de sch�ma, le bout de code suivant montre que le DataReader cr�� en interne le sch�ma des colonnes sur la base de la commande et de la structure des donn�es du fichier Xml .
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
                      foreach (DataColumn col in dt.Columns)
                      {
                          //*** textBox multiligne : Tbx ***
                          tbx.AppendText("Nom de la colonne : " + col.ColumnName + Environment.NewLine);
                          tbx.AppendText("Position Ordinale : " + col.Ordinal + Environment.NewLine);
                          tbx.AppendText("Le type DataType : " + col.DataType + Environment.NewLine);
                          tbx.AppendText("En lecture seule ? : " + col.ReadOnly + Environment.NewLine);
                          tbx.AppendText("********************************************" + Environment.NewLine);
                      }
    La m�thode Load, lui permet de remplir une DataTable avec les donn�es extraites du fichier xml, tout en appliquant automatiquement son sch�ma aux colonnes de cette DataTable.


    Mais apparemment, si on code un sch�ma par l�interm�diaire de la m�thode GetSchemaTable, dans une connexion Xml il semble qu'il faille red�finir les colonnes de la DataTable.
    leur nom pour le moins et bien s�r les propri�t�s modifi�es, ici le type et le "IsReadOnly", (false par d�faut que l�on passe � true).
    Et ensuite appeler la m�thode Load.

    Code avec les propri�t�s modifi�es du sch�ma :
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
                    using (XmlDataReader xmlDataReader = cmd.ExecuteReader())
                    {
                        DataTable dtSchema = xmlDataReader.GetSchemaTable()!;
                        DataTable dt = new();
     
                        foreach (DataRow row in dtSchema.Rows)
                        {
                            DataColumn column = new(row["ColumnName"].ToString(), (Type)row["DataType"])
                            {
                                ReadOnly = (bool)row["IsReadOnly"]
                            };
     
                            dt.Columns.Add(column);
                        }
     
                        dt.Load(xmlDataReader);
     
                        dataGridView1.DataSource = dt;
                    }

  8. #8
    Expert confirm�
    Avatar de StringBuilder
    Homme Profil pro
    Chef de projets
    Inscrit en
    F�vrier 2010
    Messages
    4 197
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 46
    Localisation : France, Rh�ne (Rh�ne Alpes)

    Informations professionnelles :
    Activit� : Chef de projets
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : F�vrier 2010
    Messages : 4 197
    Billets dans le blog
    1
    Par d�faut
    Bonjour,

    J'avoue que j'ai pas tout compris tes messages chrismonoye

    En fait, ce que je ne comprends pas, c'est pourquoi quand GetSchemaTable() retourne null, il est capable de faire un Load() en auto-g�n�rant un sch�ma qui fonctionne, alors que lorsque je cr�e un joli sh�ma roul� sous les aisselles il plante comment un demeur� lors du .Load()...

    Par contre, en suivant tes tests, j'ai pu valider qu'il y avait clairement un truc entre le sch�ma que je cr�ais et le sch�ma n�cessaire pour cr�er les colonnes dans le datatable de destination, puisque si on les cr�e �a la main, �a ne plante pas.

    Combin� � l'impl�mentation de la m�thode trouv�e ici https://www.sqlnotes.info/2012/02/13...g-idatareader/ je me suis rendu compte que si j'allais plus loin que les quelques lignes propos�es par la doc Microsoft, et qu'on ajoutait quelques infos au sch�ma, �a ne plantait plus.

    Au final, �a coulait de source, c'est juste domage que le message d'erreur �tait si peu parlant...

    Mon sch�ma indique que les colonnes sont de type "string"... Hors, vu que c'est un connecteur pour base de donn�es, un string se transforme en "((n)var)char(zz)"... Et � aucun moment je ne fourni la taimme de la cha�ne !

    Allez, petit ajout dans le GetSchema :
    Code csharp : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
            public DataTable? GetSchemaTable()
            {
                DataTable schemaTable = new();
                schemaTable.Columns.Clear();
                schemaTable.Rows.Clear();
                schemaTable.Columns.Add("ColumnName", typeof(string));
                schemaTable.Columns.Add("ColumnOrdinal", typeof(int));
                schemaTable.Columns.Add("DataType", typeof(Type));
                schemaTable.Columns.Add("IsReadOnly", typeof(bool));
                schemaTable.Columns.Add("ColumnSize", typeof(int));
     
                for (int i = 0; i < FieldCount; i++)
                {
                    DataRow row = schemaTable.NewRow();
                    row.BeginEdit();
                    row["ColumnName"] = GetName(i);
                    row["ColumnOrdinal"] = i;
                    row["DataType"] = GetFieldType(i);
                    row["IsReadOnly"] = true;
                    row["ColumnSize"] = -1;
                    row.EndEdit();
                    schemaTable.Rows.Add(row);
                }
     
                return schemaTable;
            }

    On note l'ajout de la colonne "ColumnSize" avec une valeur de -1 et le tour est jou� !

+ R�pondre � la discussion
Cette discussion est r�solue.

Discussions similaires

  1. [AJAX] Loading sur Ajax?
    Par naima2005 dans le forum G�n�ral JavaScript
    R�ponses: 1
    Dernier message: 04/09/2007, 23h55
  2. dataTable : onmouseover sur les <tr>
    Par laurent.c123 dans le forum JSF
    R�ponses: 6
    Dernier message: 17/07/2007, 11h11
  3. [1.1][C#] Problème de load sur lien
    Par neramo dans le forum ASP.NET
    R�ponses: 3
    Dernier message: 20/11/2006, 19h36
  4. Lazy loading sur component
    Par El Saigneur dans le forum Hibernate
    R�ponses: 2
    Dernier message: 03/11/2006, 10h30
  5. R�ponses: 9
    Dernier message: 15/07/2006, 12h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo