Pages

Men

rh

7/30/2014

|How to write Dataset data to file in c#

using System. Text;
using System.Data;
using System. IO;




public class WriteDataSettoFile
    {
        String filePath=String.Empty;
        public bool WriteDataSet ( String path , DataSet ds )
        {
            try
            {
                filePath = path;
                WriteLine ( ds. DataSetName );
                foreach ( DataTable dt in ds. Tables )
                {

                    WriteLine ( dt. TableName );
                    for ( int curCol = 0; curCol < dt. Columns. Count; curCol++ )
                    {
                        WriteLine ( dt. Columns [ curCol ]. ColumnName. Trim ( ) + "\t" );
                    }
                    for ( int curRow = 0; curRow < dt. Rows. Count; curRow++ )
                    {
                        for ( int curCol = 0; curCol < dt. Columns. Count; curCol++ )
                        {
                            WriteLine ( dt. Rows [ curRow ] [ curCol ]. ToString ( ). Trim ( ) + "\t" );
                        }
                        WriteLine ( Environment. NewLine );
                    }
                }
                return true;
            }
            catch ( IOException ie )
            {
                WriteLine ( ie. Message );
                return false;
            }
        }
        private void WriteLine ( String dataString )
        {
            if ( !File. Exists ( filePath) )
            {
                using ( StreamWriter sw = File. CreateText ( filePath ) )
                {
                    //sw. WriteLine ( "Error Log File Created ON:" + DateTime. Now. ToString ( "dd/MMM/yy" ) );
                }
            }

            using ( StreamWriter sw = File. AppendText ( filePath ) )
            {
                sw. WriteLine ( dataString );
            }

        }
    }

1 comment :

  1. Borgata Hotel Casino & Spa - JTR Hub
    Located in Atlantic City, 출장안마 Borgata Hotel https://septcasino.com/review/merit-casino/ Casino & Spa offers the finest in amenities and entertainment. It also provides a seasonal outdoor kadangpintar swimming

    ReplyDelete