[英]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.