Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace ExportDataTableApplication
{
    class ExportDataTable
    {
        DateTime datetime = DateTime.Now;
        //Execute stored procedure
        static void Main(string[] args)
        {
            Console.WriteLine("Running App");

            DataSet ds = new DataSet();
            DateTime datetime = DateTime.Now;

            using (SqlConnection conn = new SqlConnection("Test connection string"))
            {

                SqlCommand sqlComm = new SqlCommand("Test", conn);

                sqlComm.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(sqlComm);

                da.Fill(ds);

            }
            //create csv file
            StreamWriter sw = null;
            StringBuilder sb = new StringBuilder();

            foreach (DataTable dt in ds.Tables)
            {

                sw = new StreamWriter(string.Format(@"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\CSV File\Export{0}" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv", dt.TableName));

                // Write data table columns
                int iColCount = dt.Columns.Count;

                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }

                sw.Write(sw.NewLine);

                // Write all data table rows.
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if ((!Convert.IsDBNull(dr[i])) && (i == 7))
                        {
                            sw.Write(dr[i].ToString().Replace("\r", " "));
                        }
                        else
                        {
                            sw.Write(dr[i].ToString().Replace(",", " "));
                        }

                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }

                    sw.Write(sw.NewLine);
                }

                sw.Close();
            }
            ExportDataTable program = new ExportDataTable();
            program.log(" ");

            Console.WriteLine("App ran successfully");
        }

        //Create log file

        public void log(String message)
        {

            String oFileName = @"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\Log\Log_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".txt";
            if (!File.Exists(oFileName))
            {
                System.IO.FileStream f = File.Create(oFileName);
                f.Close();
            }

            try
            {
                System.IO.StreamWriter writter = File.AppendText(oFileName);
                writter.WriteLine(datetime.ToString("dd-MM-yyyy_hh-mm-ss") + " > " + message );
                writter.Flush();
                writter.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }

    }

}
Posted
Updated 3-Aug-15 3:30am
v7

That's because nowhere a call for the log function...
 
Share this answer
 
Comments
Member 11878313 3-Aug-15 6:40am    
I treid calling with
ExportDataTable program = new ExportDataTable();
program.log();

However :
Error 1:No overload for method 'log' takes 0 arguments
Kornfeld Eliyahu Peter 3-Aug-15 6:42am    
You actually read code before using it? log function must get a parameter!
Member 11878313 3-Aug-15 6:45am    
Can you explain abit more?
Kornfeld Eliyahu Peter 3-Aug-15 6:46am    
public void log(String message)
Member 11878313 3-Aug-15 6:52am    
hmmm.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;
 
namespace ExportDataTableApplication
{
    class ExportDataTable
    {
        DateTime datetime = DateTime.Now;
        //Execute stored procedure
        static void Main(string[] args)
        {
            Console.WriteLine("Running App");
			//Create object on class level//Outside from try catch
            ExportDataTable program = new ExportDataTable();
            DataSet ds = new DataSet();
            DateTime datetime = DateTime.Now;
			try	
			{
				using (SqlConnection conn = new SqlConnection("Test connection string"))
				{
	 
					SqlCommand sqlComm = new SqlCommand("Test", conn);
	 
					sqlComm.CommandType = CommandType.StoredProcedure;
	 
					SqlDataAdapter da = new SqlDataAdapter(sqlComm);
	 
					da.Fill(ds);
	 
				}
				//create csv file
				StreamWriter sw = null;
				StringBuilder sb = new StringBuilder();
	 
				foreach (DataTable dt in ds.Tables)
				{
	 
					sw = new StreamWriter(string.Format(@"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\CSV File\Export{0}" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv", dt.TableName));
	 
					// Write data table columns
					int iColCount = dt.Columns.Count;
	 
					for (int i = 0; i < iColCount; i++)
					{
						sw.Write(dt.Columns[i]);
						if (i < iColCount - 1)
						{
							sw.Write(",");
						}
					}
	 
					sw.Write(sw.NewLine);
	 
					// Write all data table rows.
					foreach (DataRow dr in dt.Rows)
					{
						for (int i = 0; i < iColCount; i++)
						{
							if ((!Convert.IsDBNull(dr[i])) && (i == 7))
							{
								sw.Write(dr[i].ToString().Replace("\r", " "));
							}
							else
							{
								sw.Write(dr[i].ToString().Replace(",", " "));
							}
	 
							if (i < iColCount - 1)
							{
								sw.Write(",");
							}
						}
	 
						sw.Write(sw.NewLine);
					}
	 
					sw.Close();
				}
				//Comment it form this place because we have created object already
				// ExportDataTable program = new ExportDataTable();
				// program.log(" ");
	 
				Console.WriteLine("App ran successfully");
			}
			catch(Exception exp)
			{
				//Call program object function log( and place here exception's property message which contains info about Error);
				//It will log 
				program.log(exp.Message);
			}            
        }
 
        //Create log file

        public void log(String message)
        {
 
            String oFileName = @"C:\Users\Desktop\ConsoleApplication2\ConsoleApplication2\Log\Log_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".txt";
            if (!File.Exists(oFileName))
            {
                System.IO.FileStream f = File.Create(oFileName);
                f.Close();
            }
 
            try
            {
                System.IO.StreamWriter writter = File.AppendText(oFileName);
                writter.WriteLine(datetime.ToString("dd-MM-yyyy_hh-mm-ss") + " > " + message );
                writter.Flush();
                writter.Close();
            }
 
            catch (Exception e)
            { 
                Console.WriteLine(e.Message.ToString());
            }
        }
 
    }
 
}


If you find any issue then let me know.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900