简体   繁体   中英

SQlBulkCopy The given value of type DateTime from the data source cannot be converted to type int of the specified target column

I am receiving the above error code when attempting to do a SqlBulkInsert of the following "Cities" DataTable:

DataTable cityTable = new DataTable(City.TABLE_NAME);
cityTable.Columns.Add("id", typeof(int));
cityTable.Columns.Add("name", typeof(string));
cityTable.Columns.Add("ascii_name", typeof(string));
cityTable.Columns.Add("alternate_names", typeof(string));
cityTable.Columns.Add("latitude", typeof(double));
cityTable.Columns.Add("longitude", typeof(double));
cityTable.Columns.Add("feature_class", typeof(char));
cityTable.Columns.Add("feature_code", typeof(string));
cityTable.Columns.Add("country_code", typeof(string));
cityTable.Columns.Add("country_code2", typeof(string));
cityTable.Columns.Add("population", typeof(long));
cityTable.Columns.Add("elevation", typeof(int));
cityTable.Columns.Add("modification_date", typeof(DateTime));
cityTable.Columns.Add("admin1code", typeof(string));
cityTable.Columns.Add("admin2code", typeof(string));
cityTable.Columns.Add("admin3code", typeof(string));
cityTable.Columns.Add("admin4code", typeof(string));
cityTable.Columns.Add("gtopo30", typeof(int));
cityTable.Columns.Add("timezone_name", typeof(string));
cityTable.Columns.Add("version", typeof(Binary));

The following is the code that adds each entity to the DataTable:

object id = EvaluateNullity(parsedCity.Id);
object name = EvaluateNullity(parsedCity.Name);
object asciiName = EvaluateNullity(parsedCity.AsciiName);
object alternateNames = EvaluateNullity(parsedCity.AlternateNames);
object latitude = EvaluateNullity(parsedCity.Latitude);
object longitude = EvaluateNullity(parsedCity.Longitude);
object featureClass = EvaluateNullity(parsedCity.FeatureClass);
object featureCode = EvaluateNullity(parsedCity.FeatureCode);
object countryCode = EvaluateNullity(parsedCity.CountryCode);
object countryCode2 = EvaluateNullity(parsedCity.CountryCode2);
object population = EvaluateNullity(parsedCity.Population);
object elevation = EvaluateNullity(parsedCity.Elevation);
object modificationDate = EvaluateNullity(parsedCity.ModificationDate);
object admin1Code = EvaluateNullity(parsedCity.Admin1Code);
object admin2Code = EvaluateNullity(parsedCity.Admin2Code);
object admin3Code = EvaluateNullity(parsedCity.Admin3Code);
object admin4Code = EvaluateNullity(parsedCity.Admin4Code);
object gtopo30 = EvaluateNullity(parsedCity.Gtopo30);
object timeZoneName = EvaluateNullity(parsedCity.TimeZoneName);
object version = EvaluateNullity(parsedCity.Version);

cityRow["id"] = id;
cityRow["name"] = name;
cityRow["ascii_name"] = asciiName;
cityRow["alternate_names"] = alternateNames;
cityRow["latitude"] = latitude;
cityRow["longitude"] = longitude;
cityRow["feature_class"] = featureClass;
cityRow["feature_code"] = featureCode;
cityRow["country_code"] = countryCode;
cityRow["country_code2"] = countryCode2;
cityRow["population"] = population;
cityRow["elevation"] = elevation;
cityRow["modification_date"] = modificationDate;
cityRow["admin1code"] = admin1Code;
cityRow["admin2code"] = admin2Code;
cityRow["admin3code"] = admin3Code;
cityRow["admin4code"] = admin4Code;
cityRow["gtopo30"] = gtopo30;
cityRow["timezone_name"] = timeZoneName;
cityRow["version"] = version;

And the code for EvaluateNullity:

    public object EvaluateNullity(object entity)
    {
        return entity ?? DBNull.Value;
    }

My understanding from this error message is that A DateTime value is being placed in one of the above int columns. However, a quick conditional debug later, and checks in the Immediate Window reveal that none of the int columns ever have DateTime types being placed in them. http://desmond.imageshack.us/Himg42/scaled.php?server=42&filename=mod1rm.jpg&res=landing http://desmond.imageshack.us/Himg37/scaled.php?server=37&filename=modyf.jpg&res=landing

I am really stumped.

From David Andres:

Double check that the order of the columns defined in cityTable matches the order of columns within the database table itself.

此错误的一个不明显的原因是您必须有一个表示标识列的属性,即使您可以不设置它们。

For me it was a mismatch between the entity framework model class and the actual table (forgot to scaffold the table model from db):

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Databases
{
    using System;
    using System.Collections.Generic;
    
    public partial class FooTable
    {
        public int Id { get; set; }
        public string Name { get; set; }
        // --> Missing the LastUpdated Column.
    }
}

The Table: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM