SQlBulkCopy 无法将数据源中 DateTime 类型的给定值转换为指定目标列的 int
技术问答
249 人阅读
|
0 人回复
|
2023-09-12
|
我正在尝试执行以下城市数据表 SqlBulkInsert 收到上述错误代码:
( |# [1 x9 L* e; eDataTable 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));以下是将每个实体添加到 DataTable 的代码:" l4 k4 Y) |7 I2 z! h. x0 W
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;以及 EvaluateNullity 的代码:
4 I# I6 i2 y- l; p public object EvaluateNullity(object entity) return entity ?? DBNull.Value; }我对这个错误消息的理解是 DateTime 值放在上面 int 列之一。但是,以后进行快速条件调试,并窗口,结果显示所有 int DateTime 类型。/ T. b" s- F- `8 ~. H1 d9 n- h$ p- W
) C" H2 |6 R6 z9 M+ K8 Q 解决方案:
, {. T! ]% z* \8 y' A8 S# P/ e 仔细检查 cityTable 中定义的列顺序是否与数据库表本身中的列顺序相匹配。 |
|
|
|
|
|