Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Serialize SqlDataReader result to XML
Message
De
15/01/2013 04:28:49
 
 
À
15/01/2013 04:26:22
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01562506
Message ID:
01562542
Vues:
25
>>>Hi everybody,
>>>
>>>I am reading some code
>>>
>>>
>>>// With SqlDataReader, you don't know how many records their are unless you count them.
>>>            recordCount = 0;
>>>            try
>>>            {
>>>                // StringBuilder for the entire return string and one for the records
>>>                StringBuilder returnValue = new StringBuilder();
>>>                StringBuilder recordValues = new StringBuilder();
>>>
>>>                // Add the type and the Table start tags
>>>                returnValue.Append("<XML>");
>>>                returnValue.Append("<Table>");
>>>
>>>                // Loop through the records and get the column names, values, and record count
>>>                while (sqlDataReader.Read())
>>>                {
>>>                    // There is no sqlDataReader.RecordCount property - we need to increment a counter to figure it out
>>>                    recordCount++;
>>>
>>>                    // Add the Record tag
>>>                    recordValues.Append("<Record>");
>>>
>>>                    // Loop through the columns and get the name and value
>>>                    for (Int32 i = 0; i < sqlDataReader.FieldCount; i++)
>>>                    {
>>>                        // Store the appropriate format of the column name based on return type
>>>                        String columnName = sqlDataReader.GetName(i);
>>>
>>>                        // Get the column value based on the column type
>>>                        String columnValue = GetColumnValue(sqlDataReader, i, true);
>>>
>>>                        // Set the string for the record in the appropriate format (removing any empty values from the return)
>>>                        recordValues.AppendFormat("<{0}>{1}</{0}>", columnName, columnValue);
>>>                    }
>>>
>>>                    // The end Record tag
>>>                    recordValues.Append("</Record>");
>>>                }
>>>
>>>                // Add the record count for the XML return type
>>>                returnValue.AppendFormat("<{0}>{1}</{0}>", "RecCount", recordCount);
>>>
>>>                // Append all of the record values (already formatted)
>>>                returnValue.Append(recordValues);
>>>                returnValue.Append("</Table>");
>>>                returnValue.Append("</XML>");
>>>
>>>                // Return the whole string...
>>>                return returnValue.ToString();
>>>
>>>I am thinking there may be a way to write the above more efficiently with a fewer lines of code.
>>>
>>>Do you think it's possible or the above is as good as it gets?
>>>
>>>Thanks in advance.
>>
>>What's in GetColumnValue() - that's likely to be the slow bit ?
>>
>>Since the column names are fixed you might be able to cache those and avoid using 'sqlDataReader.GetName(i);' repeatedly.
>>
>>I assume that the required output format precludes using DataSet.WriteXML
>
>
>In addition to caching the column names, you can do it with one StringBuilder instead of two - would need an Insert()

Yeah, thought of that. It's only there to put the record count at the top
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform