如何将.Net Datetime转换为T-SQL Datetime [英] How can I convert a .Net Datetime to a T-SQL Datetime

查看:147
本文介绍了如何将.Net Datetime转换为T-SQL Datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MyDataSource.SelectParameters["startDate"].DefaultValue =
                                     fromDate.SelectedDate.Date.ToString();

正如您所看到的,我设置了.Net SqlDataSource中参数的默认值。我不认为字符串输出正确的格式。什么是正确的格式,所以T-SQL Datetime将正确匹配?

As you can see I am setting the default value of a parameter in my .Net SqlDataSource. I don't think the string is outputting the correct format though. What is the correct format so a T-SQL Datetime will match it properly?

以下是现在输出的格式:

Here are the formats that are outputted right now:


  • 5/1/2009 12:00:00 AM

  • 5/4/2009 11:59:59 PM

编辑:在我有DataSource之前应该已经写过SqlDataSource

Before I had DataSource where it should have been written SqlDataSource

这里是一些代码

<asp:SqlDataSource ID="MyDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:sampledb %>" SelectCommand="Recording_Select" SelectCommandType="StoredProcedure" OnSelecting="MyDataSource_Selecting">
    <SelectParameters>
        <asp:ControlParameter ControlID="respondentID" DefaultValue="%" Name="RespondentID" PropertyName="Text" Type="String" />
        <asp:SessionParameter Name="projectCode" SessionField="proj" Type="String" />
        <asp:ControlParameter ControlID="interviewerList" Name="InterviewerID" PropertyName="SelectedValue" Type="String" />
        <asp:ControlParameter ControlID="dispCodeList" Name="dispCode" PropertyName="SelectedValue" Type="Int32" />
        <asp:ControlParameter ControlID="fromDate" DefaultValue="1/1/1900" Name="startDate" PropertyName="SelectedValue" Type="DateTime" />
        <asp:ControlParameter ControlID="toDate" DefaultValue="1/1/1900" Name="endDate" PropertyName="SelectedValue" Type="DateTime" />
    </SelectParameters>
</asp:SqlDataSource>

编辑:
嗯,我想出了问题,看起来有点傻。代码从不调用DefaultValue,因为总是存在一个值。所以我唯一的解决办法是创建一个新的隐藏的标签控件,并加载修正的日期时间。这几乎完成了诀窍。我不知道我是如何忽视的。

Well, I figured out the problem and it will seem a bit silly. The code was never calling the DefaultValue because there was always a value present. So my only solution was to make a new hidden label control and load the corrected datetime into that. That pretty much did the trick. I don't know how I overlooked that.

推荐答案

你最初如何创建参数?您应该能够将数据类型指定为datetime类型。然后直接将datetime分配给参数,而不是尝试将其转换为字符串。

How are you initially creating the parameter? You should be able to specify the datatype as datetime at that time. Then assign your datetime to the parameter directly rather than trying to convert it to a string.

根据您发布的代码你最好的选择可能只是预先填充你的控制。另外,对于上下文中的默认值,这看起来很好,所以我怀疑这个问题在别的地方。尝试将控件的 CancelSelectOnNullParameter 属性设置为False。

Based on the code you posted, your best option is probably just to pre-populate your control. Additionally, that looks fine for the default value in that context, so I suspect the problem is somewhere else. Try setting the CancelSelectOnNullParameter property of the control to False.

这篇关于如何将.Net Datetime转换为T-SQL Datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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