开发者

Convert from string to date type in SQL server

For example: I let the user inputs date by

<asp:TextBox ID="date" runat="server" Width="20开发者_开发问答0px" Text = "20110815"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalendarExtender" runat="server" PopupPosition="BottomLeft" PopupButtonID="date"
TargetControlID="date" Format="yyyyMMdd" FirstDayOfWeek="Monday">
</ajaxToolkit:CalendarExtender>

The problem is that in my stored procedure, i tried using that string as a date type

@StartDate nvarchar(8),
@StartDate_int int =0
AS

BEGIN
select  @StartDate_int=CAST(convert(varchar(12),DATEADD(week,DATEDIFF(week,0,@StartDate),0),112) as int)
select * from table where date(has int type)// = @StartDate_int

END

When I tried testing that stored procedure in MS Server management studio, I defined the parameter for @StartDate = 20110101 and the stored procedure did return all the corrected rows, but when i tried using stored procedure in my asp.net project, set @StartDate parameter to string in the text-box, it didn't return any row.

    <asp:SqlDataSource ID="SqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SelectSpecificTimeSheet" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:SessionParameter DefaultValue="user" Name="UserName" 
            SessionField="loginState" Type="String"/>
        <asp:ControlParameter DefaultValue="20110815" ControlID="date" Name="StartDate" PropertyName="Text" Type="String"/>
        <asp:Parameter Name="StartDate_int" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

Can somebody show me what i did wrong :(


Several suggestions:

  1. You can use Sql Profiler to see what data come to Sql server (Microsoft Sql Profiler for Enterprise Sql Server or AnjLab Sql Profiler for Sql Server Express)

  2. Create separate class to make calls to sql server (repository/data manager layer) and use to access your data manager - in this way you'll always can control what is going on there


http://linesofcode.net/snippets/45

Have a look at these examples. Would be a good idea to capture input to SQL Server using Sql Profiler as @cheburek suggests so you know what your server page is passing to the RDBMS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜