如何将实时框架SqlQuery和SqlParameter中的datetime参数传递给存储过程?

时间:2021-01-14 02:18:59

This is my code on server:

这是我在服务器上的代码:

model.Grid.DataSource =
       db.Database.SqlQuery<Reports.Cardex>("[dbo].[SP_RptCardexSummary_English_1] @OwnerCode, @TypeCode, @MeasurementUnitCode, @FDate",
       new SqlParameter { ParameterName = "@OwnerCode", Value = filterList[0] },
       new SqlParameter { ParameterName = "@TypeCode", Value = filterList[1] },
       new SqlParameter { ParameterName = "@MeasurementUnitCode", Value = filterList[2] },
       new SqlParameter { ParameterName = "@FDate", Value = filterList[3] })
       .ToList();

and my procedure:

和我的程序:

ALTER PROCEDURE [dbo].[SP_RptCardexSummary_English_1] 

@OwnerCode int,
@TypeCode int,
@MeasurementUnitCode smallint,
@FDate datetime

UPDATE: I created a DateTime value by String:

更新:我按字符串创建了一个DateTime值:

var date = "2015/05/25";
var time = "20:30"
filterList.Add(Convert.ToDateTime(date + " " + time));

But when I pass a System.DateTime value, it throws an exception:

但是当我传递System.DateTime值时,它会抛出异常:

"The specified cast from a materialized 'System.DateTime' type to the 'System.String' type is not valid."

“从物化'System.DateTime'类型到'System.String'类型的指定强制转换无效。”

How should I pass it?
And interesting thing is, when I set SQL Profiler for observing and run what SQL Profiler received, It works fine!! What's the problem?

我该怎么办?有趣的是,当我设置SQL Profiler进行观察并运行SQL Profiler收到的内容时,它运行正常!有什么问题?

2 个解决方案

#1


0  

There is a lot happening in this one line of code.

这一行代码中发生了很多事情。

On the way down you are sending in a datetime to a datetime parameter.

在向下的路上,您将日期时间发送到datetime参数。

The error message is a problem converting a date to a string. Could this be on the way up? Are you trying to send a date into a string on the datagrid?

错误消息是将日期转换为字符串的问题。这可能会在上升吗?您是否尝试将日期发送到数据网格上的字符串中?

You should start by creating a unit test that executes the stored procedure.

您应该首先创建一个执行存储过程的单元测试。

#2


0  

Oops! I found out... ridiculously I forgot to change the type of one of my return data from string to Datetime!

哎呀!我发现了......可笑地,我忘了将我的一个返回数据的类型从字符串更改为Datetime!

#1


0  

There is a lot happening in this one line of code.

这一行代码中发生了很多事情。

On the way down you are sending in a datetime to a datetime parameter.

在向下的路上,您将日期时间发送到datetime参数。

The error message is a problem converting a date to a string. Could this be on the way up? Are you trying to send a date into a string on the datagrid?

错误消息是将日期转换为字符串的问题。这可能会在上升吗?您是否尝试将日期发送到数据网格上的字符串中?

You should start by creating a unit test that executes the stored procedure.

您应该首先创建一个执行存储过程的单元测试。

#2


0  

Oops! I found out... ridiculously I forgot to change the type of one of my return data from string to Datetime!

哎呀!我发现了......可笑地,我忘了将我的一个返回数据的类型从字符串更改为Datetime!