C# 导出为csv时日期格式不正确

C# 导出为csv时日期格式不正确,c#,sqlite,C#,Sqlite,我是c新手,正在尝试将表数据从sqlite 3数据库导出到csv,但csv中的日期时间格式已更改sqlite 3中的格式为: 2013-04-16 21:33:42.000 而csv中的datetime显示为: 16/04/2013 21:33:42, 这是我的代码: SQLiteConnection m_dbConnection; //m_dbConnection = new SQLiteConnection("Data Source= C:/Users/IT-Administrator/

我是c新手,正在尝试将表数据从sqlite 3数据库导出到csv,但csv中的日期时间格式已更改sqlite 3中的格式为:

2013-04-16 21:33:42.000
而csv中的datetime显示为:

16/04/2013 21:33:42,
这是我的代码:

SQLiteConnection m_dbConnection;
//m_dbConnection = new SQLiteConnection("Data Source= C:/Users/IT-Administrator/Desktop/WebMobility.db; Version=3;");

m_dbConnection = new SQLiteConnection("Data source = F:/Explor/final test/WebMobility.db; Version=3;");
m_dbConnection.Open();

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("F:/Explor/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();

DateTime列没有格式。它只是用于显示列内容的工具,用于将列值格式化为特定格式。 他说,很明显,要在CSV文件上打印您的值,您也需要做同样的事情

在内部循环中,检查列是否为datetime列,然后将其值格式化为您喜欢的格式

for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
{
    if (i > 0)
    {
         swOut.Write(",");
    }
    // Datetime column content transformed  in a formatted string....
    if(i == 1)
       value = Convert.ToDateTime(dr.Cells[i].Value).ToString("yyyy-MM-dd hh:mm:ss.fff");
    else
       ....

这将在预期日期处写入一个空列。这可能是正确的,也可能是错误的,这取决于CSV文件的要求。您可以保存预定义的值,而不是空字符串。

确切地说是哪一行?myCommand.CommandText=选择CompanyId,DateTime我认为ToString yyy/MM/dd hh:MM:ss只能在DateTime变量上强制转换,我可能错了……感谢steve的回复……方法“ToString”没有重载,只包含1个参数。。当我使用你的代码时出现了这个错误。用更新的代码?Convert.ToDateTime需要在gridview行单元格中获取对象值,并应用具有datatime值所需格式的ToString对象不能从DBNull转换为其他类型这是另一个问题,您的单元格为空或null,当然不能转换为任何类型的字符串。在这种情况下,需要另一种方法。value=dr.Cells[i].DateTime.Parsevalue.ToStringyyyy/MM/dd hh:MM:ss;这给了我errorObject不能从DBNull转换为其他类型的错误
for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
{
    if (i > 0)
    {
         swOut.Write(",");
    }
    // Datetime column content transformed  in a formatted string....
    if(i == 1)
       value = Convert.ToDateTime(dr.Cells[i].Value).ToString("yyyy-MM-dd hh:mm:ss.fff");
    else
       ....
    // Datetime column content transformed in a formatted string....
    if(i == 1)
    {
       object cellValue = dr.Cells[i].Value;
       value = (cellValue == DBNull.Value ? 
             string.Empty : Convert.ToDateTime(cellValue).ToString("yyyy-MM-dd hh:mm:ss.fff");
    }