将日期参数从 C# WinForm 传递到 SQL 存储过程
我有一个包含 2 个 dateTimePicker
控件(dtpStart
和 dtpEnd
)、一个按钮和一个 datagridview
的表单> 显示结果.datagridview
绑定到 bindingSource
控件.
I have a form with 2 dateTimePicker
controls (dtpStart
and dtpEnd
), a button, and a datagridview
to show results. The datagridview
is bound to a bindingSource
control.
我想将 dateTimePicker
控件中的两个 date
参数传递给存储过程,以便在我的 datagridview 上返回所需的范围.
I want to pass two date
parameters from the dateTimePicker
controls to the stored procedure in order to return the required scope on my datagridview.
我的存储过程如下所示:
My stored procedure looks like this:
CREATE PROC [dbo].[ProcTest](@StartDate date, @EndDate date)
AS
SELECT * FROM Test WHERE ModifiedDate BETWEEN @StartDate AND @EndDate
我的 C# 代码是:
private void button1_Click(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
var Qry = dc.ProcTest(dtpStart.Value, dtpEnd.Value);
bindingSource1.DataSource = Qry;
}
当我运行上面的代码时,我的数据网格上什么也没有收到,dtpEnd.value
显示:13/08/2012 02:15:29
,我假设这是一个转换问题,因为我在存储过程中使用 date
类型,而 datetimepicker
值是 dateTime
类型.
When I run the code above I receive nothing on my datagrid, the dtpEnd.value
shows: 13/08/2012 02:15:29
, I assume that this is a conversion issue since I use date
type in my stored procedure and the datetimepicker
value is a dateTime
type.
请问怎么解决这个问题?
Please, how to resolve this ?
您需要检索查询的结果,例如:
You need to retrieve the results of your query, e.g.:
bindingSource1.DataSource = Qry.ToList();
我认为问题根本不在于 Date 参数.根据以下内容支持:
I don't think the issue is with the Date parameter at all. That's supported according to: