【问题标题】:C# Web Form Application Sql CommandC# Web 窗体应用程序 Sql 命令
【发布时间】:2019-05-02 16:40:06
【问题描述】:

这是为了获取datepicker值,这个除法没有问题

<div class="frame">
    <span class="firstdp"><span class="innerdp">
        <ej:DatePicker ID="daterange1" runat="server" ClientSideOnClose="selectedDate1" Width="80%" DateFormat="yyyy-MM-dd" Value="2017-01-01"></ej:DatePicker>         
    </span>

    </span><span class="seconddp"><span class="innerdp">
        <ej:DatePicker ID="daterange2" runat="server" ClientSideOnClose="selectedDate2"  Width="80%" DateFormat="yyyy-MM-dd" Value="2018-06-01"></ej:DatePicker>
    </span></span>
</div>

但在这种情况下,当我想获取 SQL 查询的 datepicker 值时,即 daterange1 和 daterange2,每次我遇到以下部分的崩溃时。

<asp:SqlDataSource runat="server" ID="SqlDataSource1" 
     ConnectionString='<%$ ConnectionStrings:Valeo_DBConnectionStringSUNUCU %>'    
     SelectCommand="SELECT Id, Quantity, Code AS Reference, LotNo, Date, Supplier, ISNULL(ProductionType, '-') AS ProductionType, ISNULL(Shelf, '-') AS Shelf, ISNULL(Indis, '-') AS Indis, ProjectName, ISNULL([From], '-') AS [From], ISNULL([To], '-') AS [To], Whose AS Responsible, Type
                    FROM V_Stock
                    WHERE (Type = 'Entry' AND Date >= @'dateForSql1' AND Date <= @'dateForSql2') 
                    UNION ALL
                    SELECT StockId AS Id, Quantity, Reference, LotNo, Tarih, Supplier, ISNULL(ProductionType, '-') AS ProductionType, ISNULL(Shelf, '-') AS Shelf, ISNULL(Indis, '-') AS Indis, ProjectName, ISNULL([From], '-') AS [From], ISNULL([To], '-') AS [To], Responsible, Type
                    FROM V_StockMovements  
                    ORDER BY ID DESC">
    <FilterParameters>
        <asp:FormParameter FormField="daterange1" Name="dateForSql1" Type="DateTime" DbType="DateTime" />
        <asp:FormParameter FormField="daterange2" Name="dateForSql2" Type="DateTime" DbType="DateTime" />
    </FilterParameters>                    
</asp:SqlDataSource> 

【问题讨论】:

  • 我不会直接在 aspx 文件中执行此操作。如果您尝试使用 aspx 中的嵌入式 sql 解决问题,您将遇到很多问题。我建议把它放在你的 C# 代码之外!
  • 错误是什么?
  • 错误是'必须声明标量变量“@dateForSql1”'。
  • 正如@ChristianMüller 所说,使用 aspx.cs 页面进行 SQL 查询,然后您可以使用变量(daterange1.Value 和 daterange2.Value)引用您的两个日期字段,或者直接嵌入(较少推荐)或使用 SQL 参数(推荐)。
  • 我知道谢谢推荐,但我的框架是同步融合,一开始很慢。当我想添加一些工具,如按钮、标签、日期选择器等时,程序不会自动构建。所以我无法获得我尝试过的 datepicker 事件,但它在 .cs 方面没有受到影响。它可以看到但不可编辑,因为我需要在 aspx 端制作它。

标签: c# asp.net web-applications


【解决方案1】:

您可能需要从参数名称中删除单引号。此外,对 2 个日期范围内的记录使用 Between。如下:将日期格式更改为 SQL 参数的 MM/DD/YYYY。

 Date Between @dateForSql1 AND  @dateForSql2

此外,您可以在 SqlParameter 中设置默认值。

【讨论】:

  • 我使用了这个,但在这种情况下,我得到了新错误,即“必须为“@datasql1”声明标量变量”
【解决方案2】:

你的问题的解决方案,据我所知,我可以重建它。您不应该使用“FilterParameters”而是“SelectParameters”在查询中使用标量变量。

这个结构对我来说是概念证明:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <asp:SqlDataSource runat="server" ID="SqlDataSource1" 
                       ConnectionString='Server=.;Database=Demo1;Trusted_Connection=True;'    
                       SelectCommand="SELECT * FROM tblDemo1 where date>@DateForSql1 and date<@DateForSql2">
        <SelectParameters>
            <asp:FormParameter FormField="daterange1" Name="DateForSql1" Type="DateTime" DefaultValue="01/01/2001"/>
            <asp:FormParameter FormField="daterange2" Name="DateForSql2" Type="DateTime" DefaultValue="01/01/2019"/>
        </SelectParameters>                    
    </asp:SqlDataSource> 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                  DataSourceID="SqlDataSource1" GridLines="Horizontal">
        <Columns>
            <asp:BoundField DataField="Date" HeaderText="Date"/>
            <asp:BoundField DataField="Type" HeaderText="Type"/>
        </Columns>
    </asp:GridView>
</asp:Content>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-12
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多