存储过程:从字符串转换日期和/或时间时转换失败

时间:2015-11-06 18:09:51

标签: asp.net stored-procedures devexpress

我收到错误: Conversion failed when converting date and/or time from character string

我的商店程序:

USE [DEMO]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[demo] @from_day varchar(20), @to_day varchar(20)
AS
BEGIN
select db.*
from db
where
convert(date,db.DateUse,103) between convert(date, @from_day,103) and convert(date, @to_day,103)

END

我使用ASPxDateEdit(DevExpress)和2个参数:

<dx:ASPxDateEdit ID="from_day" runat="server" ClientInstanceName="from_day" 
                        EditFormat="Custom" EditFormatString="dd/MM/yyyy"   Width="100px"  
                        Theme="Aqua"  > 
</dx:ASPxDateEdit>

<dx:ASPxDateEdit ID="to_day" runat="server" ClientInstanceName="to_day" 
                         EditFormat="Custom" EditFormatString="dd/MM/yyyy" Width="100px" 
                         Theme="Aqua"> 
</dx:ASPxDateEdit>

我使用SelectParameters:

<asp:SqlDataSource ID="SqlDataSourceGrid" runat="server" ConnectionString="<%$ ConnectionStrings:DEMOConn %>" SelectCommand="demo" SelectCommandType="StoredProcedure">
           <SelectParameters>
               <asp:ControlParameter ControlID="from_day" Name="from_day" PropertyName="Value" Type="String" />
               <asp:ControlParameter ControlID="to_day" Name="to_day" PropertyName="Value" Type="String" />
           </SelectParameters>
</asp:SqlDataSource>

我无法解决此错误。

1 个答案:

答案 0 :(得分:0)

您正在将字符串传递给SP中的datetime参数。将ControlParameters中的数据类型更改为DateTime,或将SP中的字符串转换为日期时间值。就个人而言,我改变了ControlPatameters。希望这有帮助