Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have an application running in C Sharp, its convert a data table to CSV format,
The data table values are fetched from a SQL server, my concern in the database a column for to store date and time as datetime format(yyyy-mm-dd hh:mm:ss), and its value is this format
2021-05-31 14:01:07.000
,
once data from datatable to CSV file, the date time format will automatically change to
dd/mm/yyyy hh:mm:ss format,
i want same datetime format in SQL server as in CSV file also,
How i can achieve this.

My concern HOW Windows forms converting values from SQL datetime(yyyy-mm-dd hh:mm:ss) format to different format as (dd/mm/yyyy hh:ss:mm)

What I have tried:

Code to Create CSV file is here,

public void CreateCSVFile(DataTable dt1, string strfilepath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(filepath);
                dir.Refresh();
                StreamWriter sw = new StreamWriter(strfilepath, false);
                //int columncount = dt1.Columns.Count;
                for (int i = 0; i < dt1.Columns.Count; i++)
                {
                    sw.Write(dt1.Columns[i]);
                    if (i < dt1.Columns.Count - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
                foreach (DataRow dr in dt1.Rows)
                {
                    for (int i = 0; i < dt1.Columns.Count; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            string value = dr[i].ToString();
                            if (value.Contains(','))
                            {
                                value = String.Format("\"{0}\"", value);
                                sw.Write(value);
                            }
                            else
                            {
                                sw.Write(dr[i].ToString());
                            }
                        }
                        if (i < dt1.Columns.Count - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
Posted
Updated 21-Jun-21 11:09am

1 solution

At a guess, you are storing the data in the DB as DATETIME or DATETIME2 format - if you aren't, then your DB is badly designed and needs to be fixed.

And when you look at it as DATETIME data, it shows in the default format for that machine, which is ISO. The data itself has no format - it's stored as a number of ticks since a specific point in time and only "acquires" a format when it it s=converted to a string.

So when you read it into your app, it gets read into a DateTime column in the DT, and you app then generates a CSV file from that. And the default ToString implementation produces a string which is formatted according to the setting for the computer that the app is running on - which looks like it's European format: day first.

You can specify the format for ToString to generate any format you need and override the default setting: Formatting a DateTime for display - format string description[^]
 
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