You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置decimal类型字段为0时触发TCP传输层错误的求助

问题描述

执行C#代码向TMateriales表插入数据时,当decimal类型的PriceMateriales和PricePecMateriales字段设为0时,触发传输层错误。

代码片段

using (var command = new SqlCommand("INSERT INTO TMateriales (NumMateriales, CodMateriales, NamMateriales, IslocalPMateriales, IdCategries, IsDepoMateriales, MadMateriales, PriceMateriales, PricePecMateriales, IdCurrency, TotaAmountOfPiece, DatMateriales) VALUES (@NumMateriales, @CodMateriales, @NamMateriales, @IslocalPMateriales, @IdCategries, @IsDepoMateriales, @MadMateriales, @PriceMateriales, @PricePecMateriales, @IdCurrency, @TotaAmountOfPiece, @DatMateriales)", App.Connection))
{
    command.Parameters.AddWithValue("@NumMateriales", row.NumMateriales ?? string.Empty);
    command.Parameters.AddWithValue("@CodMateriales", row.CodMateriales ?? string.Empty);
    command.Parameters.AddWithValue("@NamMateriales", row.NamMateriales ?? string.Empty);
    command.Parameters.AddWithValue("@IslocalPMateriales", row.IslocalPMateriales ?? false);
    command.Parameters.AddWithValue("@IdCategries", row.IdCategries ?? 0);
    command.Parameters.AddWithValue("@IsDepoMateriales", row.IsDepoMateriales ?? false);
    command.Parameters.AddWithValue("@MadMateriales", row.MadMateriales ?? string.Empty);
    command.Parameters.AddWithValue("@PriceMateriales", row.PriceMateriales ?? 0);
    command.Parameters.AddWithValue("@PricePecMateriales", row.PricePecMateriales ?? 0);
    command.Parameters.AddWithValue("@IdCurrency", row.IdCurrency ?? 0);
    command.Parameters.AddWithValue("@TotaAmountOfPiece", row.TotaAmountOfPiece ?? 0);
    command.Parameters.AddWithValue("@DatMateriales", row.DatMateriales ?? DateTime.Now);

    var result = command.ExecuteScalar();
    return Task.FromResult(result != null ? Convert.ToInt32(result) : 0);
}

错误信息

a transport-level error has occurred when receiving results from the server. (provider: tcp provider, error: 0 - no such file or directory)

可能原因及解决办法

1. 参数类型不匹配(核心嫌疑)

AddWithValue会根据传入值的类型自动推断数据库参数类型,当用row.PriceMateriales ?? 0时,默认0是int类型,而数据库字段是decimal,这种隐性类型转换可能触发底层传输异常。

  • 解决:显式指定参数类型,替换AddWithValue为Add并明确SqlDbType,同时用0m表示decimal类型的0:
    // 替换原Price字段的参数添加代码
    command.Parameters.Add("@PriceMateriales", SqlDbType.Decimal).Value = row.PriceMateriales ?? 0m;
    command.Parameters.Add("@PricePecMateriales", SqlDbType.Decimal).Value = row.PricePecMateriales ?? 0m;
    

2. 数据库连接状态异常

传输层错误本质是网络/连接问题,虽仅在特定字段触发,但需排查连接本身:

  • 检查App.Connection的状态,执行前确保连接已打开且未被其他操作占用;
  • 验证连接字符串的服务器地址、端口是否正确,尝试重启SQL Server服务或客户端应用。

3. 表约束或触发器干扰

若TMateriales表存在触发器或约束,当Price字段为0时,可能触发数据库端异常导致连接中断:

  • 临时禁用表上的触发器,测试插入操作是否正常;
  • 检查表的外键、检查约束,确认0值是否违反约束规则。

4. 数据驱动版本bug

旧版本的SqlClient驱动可能存在类型处理缺陷:

  • 更新System.Data.SqlClient或Microsoft.Data.SqlClient到最新稳定版本。

内容的提问来源于stack exchange,提问作者SEKO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 13:42:39