SQLBulkCopy throws InvalidOperationException Nullable Ints

I have a DataTable that I am trying to load, but I have encountered the following exception:
"This String value from the data source cannot be converted to the int type of the specified target column."

The only reason I can think that this is possible is because empty cells are loaded into the DataTable, which are loaded into a column of type INT. I am trying to toggle all empty values ​​for DBNull.Value, but this does not work for this particular column. The download process is as follows:

private void UploadTable(string tableName, DataTable table) { foreach (DataRow r in table.Rows) { foreach (DataColumn c in table.Columns) if (r[c].ToString().Trim() == "") r[c] = DBNull.Value; } using (SqlBulkCopy copy = new SqlBulkCopy(sqlCon)) { copy.DestinationTableName = tableName; try { copy.WriteToServer(table); } catch (Exception ex) { lblError.Content = ex.ToString(); } } } 

I load the following diagram:

 [dbo].[table]( [Date] [smalldatetime] NOT NULL, [Dollar] [varchar](50) NULL, [CName] [varchar](100) NULL, [tick] [varchar](10) NULL, [id1] [varchar](10) NOT NULL, [Total Quantity] [int] NULL, [Quantity] [int] NULL ) 

Is there anything special about SQLBulkCopy and nullable int coolumns that I don't know about? Any input is appreciated.

+4
source share
2 answers

Poor file input caused errors - had decimal places in it and should not have

0
source

Did the column type indicate an int when you add datacolumns to a datatable?

+2
source

Source: https://habr.com/ru/post/1341198/


All Articles