简体   繁体   English

SqlBulkCopy引发异常:无法将数据源中String类型的给定值转换为指定目标列的bigint类型

[英]SqlBulkCopy throws exception: The given value of type String from the data source cannot be converted to type bigint of the specified target column

I'm trying for the first time to use SqlBulkCopy . 我正在第一次尝试使用SqlBulkCopy I have a table created this way: 我有这样创建的表:

IF NOT EXISTS(SELECT 1 FROM sys.tables 
              WHERE Name = N'DetectionLoggingTime' 
                AND Object_ID = Object_ID(N'dbo.DetectionLoggingTime'))
BEGIN
    PRINT N'Creating [dbo].[DetectionLoggingTime]...';

    CREATE TABLE [dbo].[DetectionLoggingTime]
    (
        [LogId]       INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
        [ScreeningId] BIGINT        NOT NULL,
        [Message]     NVARCHAR(255) NOT NULL,
        [Time]        BIGINT        NULL
    );
END

I am trying to insert values in this way: 我试图以这种方式插入值:

public async Task Handle(DetectionLoggingIntegrationEvent @event)
{
    var dt = new DataTable();
    dt.Columns.Add("ScreeningId");
    dt.Columns.Add("Message");
    dt.Columns.Add("Time");

    @event.OperationTimePairs.ForEach(pair => dt.Rows.Add(@event.ScreeningId, pair.Key, pair.Value));

    using (var sqlBulk = new SqlBulkCopy(_configProvider.MyConnectionString))
    {
        sqlBulk.DestinationTableName = "DetectionLoggingTime";
        sqlBulk.WriteToServer(dt);
    }
}

My feeling is that the insertion is trying to insert the LogId which I would like it to be incremental and automatically generated by SQL Server. 我的感觉是插入正在尝试插入LogId ,我希望它是增量的并由SQL Server自动生成。 What am I doing wrong? 我究竟做错了什么?

Found the solution, explicit column mappings are required: 找到解决方案后,需要显式的列映射:

public async Task Handle(DetectionLoggingIntegrationEvent @event)
{
    var dt = new DataTable();
    dt.Columns.Add("ScreeningId", typeof(long));
    dt.Columns.Add("Message");
    dt.Columns.Add("Time", typeof(long));

    @event.OperationTimePairs.ForEach(pair => dt.Rows.Add(@event.ScreeningId, pair.Key, pair.Value));

    using (var sqlBulk = new SqlBulkCopy(_configProvider.FdbConnectionString))
    {
        var mapping = new SqlBulkCopyColumnMapping("ScreeningId", "ScreeningId");
        sqlBulk.ColumnMappings.Add(mapping);

        mapping = new SqlBulkCopyColumnMapping("Message", "Message");
        sqlBulk.ColumnMappings.Add(mapping);

        mapping = new SqlBulkCopyColumnMapping("Time", "Time");
        sqlBulk.ColumnMappings.Add(mapping);

        sqlBulk.DestinationTableName = "DetectionLoggingTime";
        sqlBulk.WriteToServer(dt);
    }
}

That issue is often caused before the column type in the DataTable are not explicit. 该问题通常是在DataTable中的列类型不明确之前引起的。

By example, setting the column type might fix that error: 例如,设置列类型可能会解决该错误:

var dt = new DataTable();
dt.Columns.Add("ScreeningId", typeof(long));
dt.Columns.Add("Message");
dt.Columns.Add("Time", typeof(long));

More documentation about this issue here: https://sqlbulkcopy-tutorial.net/type-a-cannot-be-converted-to-type-b 有关此问题的更多文档,请参见: https : //sqlbulkcopy-tutorial.net/type-a-cannot-be-converted-to-type-b

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SqlBulkCopy - 来自数据源的 String 类型的给定值无法转换为指定目标列的货币类型 - SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column SQlBulkCopy 无法将数据源中 DateTime 类型的给定值转换为指定目标列的 int 类型 - SQlBulkCopy The given value of type DateTime from the data source cannot be converted to type int of the specified target column 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型 - The given value of type String from the data source cannot be converted to type nvarchar of the specified target column 来自数据源的String类型的给定值不能转换为指定目标列的varbinary类型 - The given value of type String from the data source cannot be converted to type varbinary of the specified target column 数据源中String类型的给定值无法转换为指定目标列的int类型 - The given value of type String from the data source cannot be converted to type int of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。 - The given value of type String from the data source cannot be converted to type int of the specified target column.' SQL批量复制“使用ASP.NET无法将数据源中String类型的给定值转换为指定目标列的类型日期时间” - SQL Bulk Copy “The given value of type String from the data source cannot be converted to type datetime of the specified target column” using ASP.NET 在LINQ dat source / gridview中使用文本框值作为参数:“String”类型的值无法转换为“Double”类型 - Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double' 未处理的异常:System.ArgumentException:无法从源类型扩展到目标类型 - Unhandled Exception: System.ArgumentException: Cannot widen from source type to target type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM