Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Class design
Message
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Titre:
Divers
Thread ID:
01338574
Message ID:
01338612
Vues:
12
This message has been marked as the solution to the initial question of the thread.
>I need to create a generic class that will receive a dataset (from any source and of from any kind of reader), extract the structure of the dataset (like the AFIELDS () function in VFP) and write down the field names and the data in a text file (converting non-character fields to characters). IOW, I need to create a comma-delimited text file from any dataset that the caller will give me.
>
>I'm pretty new on .NET, so I would appreciate any suggestions you may have so that I can get started in the good direction.
>
>Working with VS 2005, C# and SQL Server 2005.
>

Here's some sample code - it's really meant for VS 2008 since it's using an extension method (eg. the dataset will suddenly appear to have a new method called ToCSV()), but it's easy to convert - remove the "this" in the parameter. It's not particularly well test - I did this mostly just as a test while playing with extension methods, but you might find it useful:
using System;
using System.Data;
using System.Text;

namespace RCSSolutions.Extensions
{
    public static class DataExport
    {
        /// <summary>
        /// Converts the passed in data table to a CSV-style string.
        /// </summary>
        /// <param name="table">Table to convert</param>
        /// <param name="delimiter">Delimiter used to separate fields</param>
        /// <param name="includeHeader">true - include headers<br/>
        /// false - do not include header column</param>
        /// <returns>Resulting CSV-style string</returns>
        public static string ToCSV(this DataTable table, string delimiter, bool includeHeader)
        {
            StringBuilder result = new StringBuilder();

            if (includeHeader)
            {
                foreach (DataColumn column in table.Columns)
                {
                    result.Append(column.ColumnName);
                    result.Append(delimiter);
                }

                result.Remove(--result.Length, 0);
                result.Append(Environment.NewLine);
            }

            foreach (DataRow row in table.Rows)
            {
                foreach (object item in row.ItemArray)
                {
                    if (item is System.DBNull)
                        result.Append(delimiter);
                    else
                    {
                        string itemAsString = item.ToString();
                        // Double up all embedded double quotes
                        itemAsString = itemAsString.Replace("\"", "\"\"");
                        // To keep things simple, always delimit with double-quotes
                        // so we don't have to determine in which cases they're necessary
                        // and which cases they're not.
                        itemAsString = "\"" + itemAsString + "\"";

                        result.Append(itemAsString + delimiter);
                    }
                }

                result.Remove(--result.Length, 0);
                result.Append(Environment.NewLine);
            }

            return result.ToString();
        }
    }
}
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform