无法使用SQL批量在SQL表中插入空值

时间:2017-08-23 07:28:08

标签: sql-server excel sqlbulkcopy

我正在尝试将Excel文件数据上传到SQL表中,

下面是我的代码

using (SqlConnection objCon = new SqlConnection(strCoon))
                {
                    objCon.Open();
                    SqlCommand sqlCommand = new SqlCommand("DeleteDump", objCon);
                    sqlCommand.CommandTimeout = 0;
                    sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;                      
                    sqlCommand.ExecuteNonQuery();       
                    using (System.Data.SqlClient.SqlBulkCopy sqlBulk = new System.Data.SqlClient.SqlBulkCopy(strCoon))
                    {
                        sqlBulk.BulkCopyTimeout = 0;
                        sqlBulk.DestinationTableName = "InformationDump";

                        for (int i = 0; i < DumpResources.GetLength(0); i++)
                        {
                            sqlBulk.ColumnMappings.Add(dt.Columns[i].ColumnName.ToString(), DumpResources[i, 1].ToString());
                        }
                        sqlBulk.WriteToServer(dt);// Getting error at this line
                    }
                }

我收到错误The given value of type String from the data source cannot be converted to type datetime of the specified target column. 我试图上传的Excel工作表包含我无法在目标表中上传的空数据。

1 个答案:

答案 0 :(得分:0)

我想问题是你试图将空值转换为字符串,然后尝试将其放在只接受日期时间的字段中。 在将其转换为字符串之前检查是否有任何东西(如此无效)。

相关问题