DataTable에서 SqlBulkCopy를 수행하려고 할 때이 예외가 발생합니다.SqlBulkCopy - 데이터 원본의 String 형식의 지정된 값을 지정된 대상 열의 money 형식으로 변환 할 수 없습니다.
Error Message: The given value of type String from the data source cannot be converted to type money of the specified target column.
Target Site: System.Object ConvertValue(System.Object, System.Data.SqlClient._SqlMetaData, Boolean, Boolean ByRef, Boolean ByRef)
나는 오류가 무슨 말을 이해하지만 어떻게 나는 그런이가 일어나고 행/필드로, 더 많은 정보를 얻을 수 있습니까? 데이터 테이블은 타사에서 채워지며 최대 200 개의 열과 최대 10,000 개의 행을 포함 할 수 있습니다. 반환되는 열은 제 3 자에게 전송 된 요청에 따라 다릅니다. 데이터 테이블 열은 모두 문자열 유형입니다.
//--- create lists to hold the special data type columns
List<DataColumn> IntColumns = new List<DataColumn>();
List<DataColumn> DecimalColumns = new List<DataColumn>();
List<DataColumn> BoolColumns = new List<DataColumn>();
List<DataColumn> DateColumns = new List<DataColumn>();
foreach (DataColumn Column in dtData.Columns)
{
//--- find the field map that tells the system where to put this piece of data from the 3rd party
FieldMap ColumnMap = AllFieldMaps.Find(a => a.SourceFieldID.ToLower() == Column.ColumnName.ToLower());
//--- get the datatype for this field in our system
Type FieldDataType = Nullable.GetUnderlyingType(DestinationType.Property(ColumnMap.DestinationFieldName).PropertyType);
//--- find the field data type and add to respective list
switch (Type.GetTypeCode(FieldDataType))
{
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64: { IntColumns.Add(Column); break; }
case TypeCode.Boolean: { BoolColumns.Add(Column); break; }
case TypeCode.Double:
case TypeCode.Decimal: { DecimalColumns.Add(Column); break; }
case TypeCode.DateTime: { DateColumns.Add(Column); break; }
}
//--- add the mapping for the column on the BulkCopy object
BulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(Column.ColumnName, ColumnMap.DestinationFieldName));
}
//--- loop through all rows and convert the values to data types that match our database's data type for that field
foreach (DataRow dr in dtData.Rows)
{
//--- convert int values
foreach (DataColumn IntCol in IntColumns)
dr[IntCol] = Helpers.CleanNum(dr[IntCol].ToString());
//--- convert decimal values
foreach (DataColumn DecCol in DecimalColumns)
dr[DecCol] = Helpers.CleanDecimal(dr[DecCol].ToString());
//--- convert bool values
foreach (DataColumn BoolCol in BoolColumns)
dr[BoolCol] = Helpers.ConvertStringToBool(dr[BoolCol].ToString());
//--- convert date values
foreach (DataColumn DateCol in DateColumns)
dr[DateCol] = dr[DateCol].ToString().Replace("T", " ");
}
try
{
//--- do bulk insert
BulkCopy.WriteToServer(dtData);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
//--- handles error
//--- this is where I need to find the row & column having an issue
}
이
이 코드는 목적지에 대한 모든 값을 포맷해야 내 데이터베이스의 열 전에 삽입을 실행에, 나는 다음과 같은 코드 (제거되지 않은 중요한 코드)를 사용하여 데이터 테이블 값을 포맷 따라서, 모든 VARCHAR 없습니다 전지. 십진수 오류가있는 경우이를 지우는 함수는 0-9 또는 0이 아닌 문자를 모두 제거합니다. (소수점). 오류를 던지고있는이 필드는 데이터베이스에서 Nullable이 될 수 있습니다.
수준이 예외는이 오류가 있습니다
Error Message: Input string was not in a correct format
Target Site: Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)
누구도 해결하기 위해 어떤 아이디어가 있습니까 :
Error Message: Failed to convert parameter value from a String to a Decimal.
Target Site: System.Object CoerceValue(System.Object, System.Data.SqlClient.MetaType, Boolean ByRef, Boolean ByRef, Boolean)
와 레벨 3 예외를이 오류가? 자세한 정보를 얻으려는 아이디어가 있습니까?
문서를 보면 이벤트를 발생시키는 ['NotifyAfter'] (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.notifyafter.aspx) 속성을 사용할 수 있습니다. N 개의 행이 복사 될 때 기본 행이있는 행의 배치를 파악하는 데 도움이 될 수 있습니다. – shahkalpesh
'CleanDecimal' 메소드가 데이터의 유효성 검사를 시도하거나 유효하지 않은 문자를 제거하려고합니까? 'Decimal.TryParse()'를 사용하여 데이터가 실제로 올바른 형식인지 확인해 보았습니까? 그에게 가서 문제가되는 가치가 나타나는지 확인하십시오. – Corey