将日期参数从 C# WinForm 传递到 SQL 存储过程 [英] Passing Date parameter from C# WinForm to SQL stored procedure

查看:39
本文介绍了将日期参数从 C# WinForm 传递到 SQL 存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 个 dateTimePicker 控件(dtpStartdtpEnd)、一个按钮和一个 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:

http://msdn.microsoft.com/en-us/library/bb386947.aspx

这篇关于将日期参数从 C# WinForm 传递到 SQL 存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆