SqlDataSource尝试运行存储过程时出错

时间:2011-11-30 14:23:54

标签: sql stored-procedures sqldatasource

当尝试测试运行存储过程的sqlDataSource时,我得到一个错误: “执行查询时出错。请检查命令的语法,如果存在,参数的类型和值,并确保它们是正确的。 无法启用约束。一行或多行包含违反非空,唯一或外键约束的值“

存储过程是:

USE [FAB28]
GO
/****** Object:  StoredProcedure [dbo].[spPLCIOFilter]    Script Date: 11/30/2011 15:39:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[spPLCIOFilter]
(
    @filter1 VARCHAR(50) = '%',
    @filter2 VARCHAR(50) = '%',
    @slot VARCHAR(50) = '%' 
)
AS
BEGIN
    SELECT
       [PLC] "PLC RIO"
      ,[SIGNAL] SIGNAL
      ,[RACK] RACK
      ,[SLOT] SLOT
      ,[POINT] POINT
      ,[CARD] CRAD
      ,[TAG] TAG
      ,[PROJ] PROJ
      ,[DESCRIPTION] "DESCRIPTION"
      ,[ADDRESS] "ADDRESS"

    FROM [FAB28].[dbo].[PLC_TAGS2]
    where [PLC] <> ''
    and [PLC] LIKE '%' + @filter1 + '%'
    and [PLC] LIKE '%' + @filter2 + '%'
    and [SLOT] LIKE '%' + @slot + '%'
    ORDER BY [PLC RIO],[RACK],[SLOT],[POINT]

END

奇怪的是,当我放置值而不是变量时,我成功地在SP内运行查询。

有人可以帮助我吗?

这是使用SP的sqlDataSource代码:

<asp:SqlDataSource ID="SqlDataSource_PLC_IO" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:FAB28ConnectionString_PlcIO %>" 
                        SelectCommand="spPLCIOFilter" SelectCommandType="StoredProcedure">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="ddl_filter1" DefaultValue="" Name="filter1" 
                                PropertyName="SelectedValue" Type="String" />
                            <asp:ControlParameter ControlID="ddl_filter2" DefaultValue="" Name="filter2" 
                                PropertyName="SelectedValue" Type="String" />
                            <asp:ControlParameter ControlID="ddl_filter_slot" DefaultValue="" Name="slot" 
                                PropertyName="SelectedValue" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

看起来ORDER BY子句不正确:

ORDER BY [PLC RIO],[RACK],[SLOT],[POINT]

[PLC RIO]不是列。将其更改为...时会发生什么?

ORDER BY [PLC],[RACK],[SLOT],[POINT]