C# 该字符串未被识别为有效的日期时间。有一个从索引0开始的未知单词。”;

C# 该字符串未被识别为有效的日期时间。有一个从索引0开始的未知单词。”;,c#,excel,sql-server-2008,datatable,C#,Excel,Sql Server 2008,Datatable,该字符串未被识别为有效的日期时间。有一个从索引0开始的未知单词。“ 我正在尝试将Excel数据导入Sql Server db表,因为我正在进行批量插入,在我的db中,数据类型为date和Excel字符串 下面是我的代码: public static class DataTableExt { public static void ConvertColumnType(this DataTable DTable, string columnName, Type newType) {

该字符串未被识别为有效的日期时间。有一个从索引0开始的未知单词。“

我正在尝试将Excel数据导入Sql Server db表,因为我正在进行批量插入,在我的db中,数据类型为
date
和Excel字符串

下面是我的代码:

public static class DataTableExt
{
    public static void ConvertColumnType(this DataTable DTable, string columnName, Type newType)
    {
        using (DataColumn dc = new DataColumn(columnName + "_new", newType))
        {
            // Add the new column which has the new type, and move it to the ordinal of the old column
            int ordinal = DTable.Columns[columnName].Ordinal;
            DTable.Columns.Add(dc);
            dc.SetOrdinal(ordinal);

            // Get and convert the values of the old column, and insert them into the new
            foreach (DataRow dr in DTable.Rows)
                if (!string.IsNullOrEmpty(dr[columnName].ToString()))
                {
                    dr[dc.ColumnName] = Convert.ChangeType( dr[columnName], newType);
                }
                else
                {
                    dr[dc.ColumnName] = Convert.ChangeType(DateTime.Now, newType);
                }

            // Remove the old column
            DTable.Columns.Remove(columnName);

            // Give the new column the old column's name
            dc.ColumnName = columnName;
        }
    }
}

查看
DateTime.ParseExact()
:查看
DateTime.ParseExact()