简体   繁体   English

System.Data.SqlClient.SqlException: '操作数类型冲突:datetime2 与十进制不兼容

[英]System.Data.SqlClient.SqlException: 'Operand type clash: datetime2 is incompatible with decimal

I have a Stored Procedure which requires a structured parameter.我有一个需要结构化参数的存储过程。 I'm trying to call the stored procedure from my C# code and I get following error.我正在尝试从我的 C# 代码中调用存储过程,但出现以下错误。

System.Data.SqlClient.SqlException: 'Operand type clash: datetime2 is incompatible with decimal The data for table-valued parameter "@TableType" doesn't conform to the table type of the parameter. System.Data.SqlClient.SqlException: '操作数类型冲突:datetime2 与十进制不兼容表值参数“@TableType”的数据不符合参数的表类型。 SQL Server error is: 200, state: 7' SQL Server 错误是:200,状态:7'

Below is my stored procedure which is available in the database.下面是我在数据库中可用的存储过程。

CREATE PROCEDURE dbo.spProcessData
    @TableType AS dbo.TblTypeHourlyData READONLY
AS
BEGIN
    SET NOCOUNT ON;


END

This is my table type script.这是我的表类型脚本。

CREATE TYPE dbo.TblTypeHourlyData AS TABLE 
(
    Name NVARCHAR(50),
    RecordDate DATETIME,
    Amount  DECIMAL(18,2)
)
GO

I call the SP by passing the required parameter (data table).我通过传递所需的参数(数据表)来调用 SP。 However, it returns an error in the var executeNonQuery = command.ExecuteNonQuery();但是,它在var executeNonQuery = command.ExecuteNonQuery();返回错误var executeNonQuery = command.ExecuteNonQuery(); line.线。

        List<HourlyData> data = .................
        using (var connection = CreateConnection())
        {
            using (var command = connection.CreateCommand())
            {
                command.CommandText = "dbo.spProcessData";
                command.CommandType = CommandType.StoredProcedure;

                DataTable paramDT = new DataTable();
                using (var reader = ObjectReader.Create(data))
                {
                    paramDT.Load(reader);
                }

                var param1 = new SqlParameter("@TableType", SqlDbType.Structured)
                {
                    TypeName = "dbo.TblTypeHourlyData",
                    Value = paramDT
                };

                command.Parameters.Add(param1);
                connection.Open();
                var executeNonQuery = command.ExecuteNonQuery(); // ERROR
                connection.Close();

                var value = executeNonQuery > 0;
                return value;
            }
        }

For the "data" variable, I already have a data set.对于“数据”变量,我已经有了一个数据集。 HourlyData class has the same structure as the table type. HourlyData类具有与表类型相同的结构。

public class HourlyData
{
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public DateTime? RecordDate{ get; set; }

        [DataMember]
        public decimal? Amount{ get; set; }
}

According to the info here you need to add an attribute so that the column order resulting from ObjectReader.Create(data) is the same as expected in the TVP.根据此处的信息您需要添加一个属性,以便ObjectReader.Create(data)产生的列顺序与 TVP 中的预期相同。

If the attribute is not defined in the source class then alphabetical order is used如果属性未在源类中定义,则使用字母顺序

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

相关问题 System.Data.SqlClient.SqlException - System.Data.SqlClient.SqlException &#39;System.Data.SqlClient.SqlException&#39; - 'System.Data.SqlClient.SqlException' System.Data.SqlClient.SQLException:将 varchar 数据类型转换为 datatime - System.Data.SqlClient.SQLException: The conversion of a varchar data type to datatime 发生类型为&#39;System.Data.SqlClient.SqlException&#39;的异常 - An exception of type 'System.Data.SqlClient.SqlException' occurred 类型为&#39;System.Data.SqlClient.SqlException的未处理异常 - An unhandled exception of type 'System.Data.SqlClient.SqlException 设置&#39;引发了&#39;System.Data.SqlClient.SqlException&#39;类型的异常 - Settings' threw an exception of type 'System.Data.SqlClient.SqlException' 类型为“ System.Data.SqlClient.SqlException”的异常实例失败 - An exception of type 'System.Data.SqlClient.SqlException' Instance failure System.Data.SqlClient.SqlException:'将 nvarchar 数据类型转换为 datetime 数据类型导致值超出范围。语句 - System.Data.SqlClient.SqlException: 'The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.The statement 数据库System.Data.SqlClient.SqlException - Database System.Data.SqlClient.SqlException System.Data.SqlClient.SqlException的奇怪情况 - Strange situation with System.Data.SqlClient.SqlException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM