将 DateTime 设置为 Gridview 的 SqlDataSource 参数 [英] Setting DateTime as a SqlDataSource parameter for Gridview
问题描述
嘿,我想用我的存储过程显示过去 30 天的某些数据.这是我所做的(aspx.cs 文件):
Hey I would like to display certain data with my stored procedure for the last 30 days. here is what I have done (aspx.cs file):
protected void Page_Load(object sender, EventArgs e)
{
DateTime toDate, fromDate;
toDate = DateTime.Now;
fromDate = toDate.Subtract(new TimeSpan(31, 0, 0, 0));
SqlDataSource1.SelectParameters.Add("fromDate", DbType.DateTime, fromDate.ToString());
SqlDataSource1.SelectParameters.Add("toDate", DbType.DateTime, toDate.ToString());
}
这是我的 aspx 文件
here is my aspx file
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" Width="232px" DataKeyNames="CustomerId" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CreationDate" HeaderText="CreationDate" SortExpression="CreationDate" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SSEnewConnectionString %>"
SelectCommand="procCustomer_SelectbyCreationDate" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DbType="DateTime" Name="fromDate" />
<asp:Parameter DbType="DateTime" Name="toDate" />
</SelectParameters>
</asp:SqlDataSource>
</form>
当我测试这个时,我的屏幕出现空白(除了母版页元素)并且没有错误.有什么想法吗?
when I test this my screen comes up blank (other than the masterpage elements) and no errors. any ideas?
推荐答案
即使接受了此答案的早期版本,但看起来我误解了所使用的参数类型.网络控件ParameterCollection
看起来有点糟糕.
Even though an earlier version of this answer was accepted, it looks like I had misunderstood the parameter type used. The web controls ParameterCollection
looks somewhat awful.
我建议将日期值转换为 SQL 格式(坦率地说,这让我很痛苦 - 应尽可能避免字符串转换).例如:
I would suggest converting date values to a SQL format (much as that pains me, frankly - string conversions should be avoided as far as possible). For example:
SqlDataSource1.SelectParameters.Add("fromDate", DbType.DateTime,
fromDate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
SqlDataSource1.SelectParameters.Add("toDate", DbType.DateTime,
toDate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
(对于仅日期类型,更改为 yyyy-MM-dd
.)
(Change to yyyy-MM-dd
for date-only types.)
我自己没有使用过 SqlDataSource
,但它也看起来就像您引入了两次参数 - 一次在标记中,一次在代码中.鉴于您在标记中没有值(包括在绑定中),您可能希望从那里删除它们 - 但我在这方面可能是错误的.
I haven't used SqlDataSource
myself, but it also looks like you're introducing the parameters twice - once in the markup, and once in the code. Given that you don't have the values in the markup (including in bindings), you may want to remove them from there - but I could be wrong on that front.
如果您的查询没有按照您的预期执行,您应该检查您的数据库日志(或任何合适的工具)以检查正在执行的实际查询.
If your query isn't doing what you expect, you should check your database logs (or whatever tool is appropriate) to check what actual query is executing.
这篇关于将 DateTime 设置为 Gridview 的 SqlDataSource 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!