SqlBulkCopy a given value of type String from a data source cannot be converted to the type nvarchar of the specified destination column.
Found on the Internet, mostly because the field in the database is too small (
A given value of type String from a data source cannot be converted to the type nvarchar of the specified destination column.
), resulting in truncation errors, after careful examination, I found that there is no field in my table design is too small, nor is it a single quotation mark problem.
After careful debugging, I found that my text file does not correspond to the table columns in the library due to the use of sqlbulkcopy import. There are 18 fields in my text file, and I have only 9 fields in my table, two of which are not in the text file (that is, the level and [Cagegory] fields in table Structure 1), and the default values are filled in by the database. The table structure is as follows:
Copy Code code as follows:
Table Structure 11
CREATE TABLE [dbo]. [Ryxx] (
[Name] [nvarchar] () not NULL,
[Idcardno] [nvarchar] () not NULL,
[Sex] [nvarchar] (2) Not NULL,
[Height] [nvarchar] (5) NULL,
[Level] [nvarchar] (2) NULL,--default is a
[Category] [nvarchar] () NULL,--default to "Focus management"
[Sponsor] [nvarchar] (MB) NULL,
[Contact] [nvarchar] (a) NULL,
[Phone] [nvarchar] (m) NULL,
[NUMBER] [nvarchar] () NULL
) on [PRIMARY]
Table Structure 1
The modified table structure is as follows
Copy Code code as follows:
CREATE TABLE [dbo]. [Ryxx] (
[Name] [nvarchar] () not NULL,
[Idcardno] [nvarchar] () not NULL,
[Sex] [nvarchar] (2) Not NULL,
[Height] [nvarchar] (5) NULL,
[Sponsor] [nvarchar] (MB) NULL,
[Contact] [nvarchar] (a) NULL,
[Phone] [nvarchar] (m) NULL,
[NUMBER] [nvarchar] (a) NULL,
[Level] [nvarchar] (2) NULL,--default is a
[Category] [nvarchar] () NULL--Default to "Focus management"
) on [PRIMARY]
So far, the problem is solved. The reason for this is because the fields obtained from the text file correspond to the default columns in the table, resulting in truncation, which should also occur if you want to use BCP.