日期格式从sqlite3更改为csv

时间:2014-03-11 12:47:33

标签: c# sqlite

我正在尝试将数据rom sqlite 3表提取到csv然后在csv文件中更改日期格式。我尝试了几乎但没有运气。我在datagridview上显示数据日期是在(20/02/2012 16:42:10,)然后导出显示为csv格式的数据(20/02/2012 16:42:10,)但是当我打开我的sqlite数据库是以其他格式(2012-02-20 16:42:10.000),我想以这种格式显示日期2012-02-20 16:42:10.000

sqlite3日期格式

日期时间

2012-02-20 16:42:10.000

csv日期格式

20/02/2012 16:42:10,

这是我的代码。

SQLiteConnection m_dbConnection;
            m_dbConnection = new SQLiteConnection("Data Source= I:/smrithi/final test/WebMobility.db; Version=3;");

           m_dbConnection.Open();
            //string DateTime = DateTime.Date.ToString("yyyy-MM-dd HH:mm:ss");
            SQLiteCommand myCommand = new SQLiteCommand();
            myCommand.Connection = m_dbConnection;


           myCommand.CommandText = "select CompanyId,DateTime,Serial,ShortDeviceId,MatricolaA,Upper(Targa),CommonRoadDescription,RoadCivicNumber,GpsAddress,VerbaliVehicleTypeDescription,VehicleBrandDescription,VehicleModelDescription,CommonColorVehicleDescription,VerbaliRuleOneCode,VerbaliRuleOneDescription,VerbaliClosedNoteDescription,VerbaliRuleOnePoints,VerbaliMissedNotificationDescription  from  VerbaliData";
            //myCommand.Connection = myConn;
            DataTable data = new DataTable();
            SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(myCommand);
            //myAdapter.SelectCommand = myCommand;
            myAdapter.Fill(data);
            dataGridView1.DataSource = data;
            this.dataGridView1.Refresh();
            if (dataGridView1.RowCount > 0)
            {
                string value = "";
                DataGridViewRow dr = new DataGridViewRow();
                StreamWriter swOut = new StreamWriter("I:/smrithi/final test/finaltest12.csv");

                //write header rows to csv
                for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
                {
                    if (i > 0)
                    {
                        swOut.Write(",");
                    }
                    swOut.Write(dataGridView1.Columns[i].HeaderText);
                }

                swOut.WriteLine();

                //write DataGridView rows to csv
                for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
                {
                    if (j > 0)
                    {
                        swOut.WriteLine();
                    }

                    dr = dataGridView1.Rows[j];

                    for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
                    {
                        if (i > 0)
                        {
                            swOut.Write(",");
                        }

                        value = dr.Cells[i].Value.ToString();
                        //replace comma's with spaces
                        value = value.Replace(',', ' ');
                        //replace embedded newlines with spaces
                        value = value.Replace(Environment.NewLine, " ");

                        swOut.Write(value);
                    }
                }
                swOut.Close();
            }


            m_dbConnection.Close();
        }

0 个答案:

没有答案