[英]Passing Date parameter from C# WinForm to SQL stored procedure
I have a form with 2 dateTimePicker
controls ( dtpStart
and dtpEnd
), a button, and a datagridview
to show results. 我有一个带有2个
dateTimePicker
控件( dtpStart
和dtpEnd
),一个按钮和一个datagridview
的表单,用于显示结果。 The datagridview
is bound to a bindingSource
control. datagridview
绑定到bindingSource
控件。
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. 我想将两个
date
参数从dateTimePicker
控件传递到存储过程,以便在我的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
My C# code is: 我的C#代码是:
private void button1_Click(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
var Qry = dc.ProcTest(dtpStart.Value, dtpEnd.Value);
bindingSource1.DataSource = Qry;
}
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. 当我运行上面的代码时,我的数据网格上什么也没收到,
dtpEnd.value
显示: 13/08/2012 02:15:29
,由于我在存储过程和datetimepicker
使用了date
类型,因此我认为这是一个转换问题。值是dateTime
类型。
Please, how to resolve this ? 请,如何解决这个问题?
You need to retrieve the results of your query, eg: 您需要检索查询的结果,例如:
bindingSource1.DataSource = Qry.ToList();
I don't think the issue is with the Date parameter at all. 我根本不认为问题与Date参数有关。 That's supported according to:
支持根据:
http://msdn.microsoft.com/en-us/library/bb386947.aspx http://msdn.microsoft.com/en-us/library/bb386947.aspx
Instead of this 代替这个
var Qry = dc.ProcTest(dtpStart.Value, dtpEnd.Value);
use this and check if your problem get solved. 使用它并检查您的问题是否得到解决。
var Qry = dc.ProcTest(dtpStart.ToString("dd-MMM-yyyy"), dtpEnd.ToString("dd-MMM-yyyy"));
Thanks to the link provided by Dave R.: http://msdn.microsoft.com/en-us/library/bb386947.aspx I found out that my code could work without any modification. 多亏了Dave R.提供的链接: http : //msdn.microsoft.com/zh-cn/library/bb386947.aspx,我发现我的代码无需任何修改即可工作。 So I searched the error first by removing parameters from SP, then I found that I had to create a new datasource object pointing to the result of the SP , and to bind it at design time to the datagridview.
因此,我首先通过从SP中删除参数来搜索错误,然后发现我必须创建一个指向SP结果的新数据源对象 ,并在设计时将其绑定到datagridview。 Now the datagrid shows data and parameters are working.
现在,数据网格显示数据和参数正在工作。
Sorry Guys, I had to verify that before posting my question, but I was very confused about Date parameters usage. 抱歉,在发布问题之前,我必须验证这一点,但是我对Date参数的使用感到非常困惑。
so I Keep the code as it is in the initial question: 所以我保留了最初问题中的代码:
private void button1_Click(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
var Qry = dc.ProcTest(dtpStart.Value, dtpEnd.Value);
bindingSource1.DataSource = Qry;
}
I beleive you need to send the date, not the value of your datetime picker. 我相信您需要发送日期,而不是日期时间选择器的值。
Basically, change this line: 基本上,更改此行:
var Qry = dc.ProcTest(dtpStart.Value, dtpEnd.Value);
To read: 读书:
var Qry = dc.ProcTest(dtpStart.SelectedDate.Value, dtpEnd.SelectedDate.Value);
If that still doesn't work I would try: 如果那仍然不起作用,我可以尝试:
var Qry = dc.ProcTest(dtpStart.SelectedDate.Value.Date, dtpEnd.SelectedDate.Value.Date);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.