代码之家  ›  专栏  ›  技术社区  ›  Rarm

使用in运算符将数字字符串用作sql查询资源的sqlparameter

  •  -2
  • Rarm  · 技术社区  · 7 年前

    我想传一根绳子 跨栏 以“,”分隔的数值:

    284409928441002841012844102284410328441042844105284410628441072844108284410928441102844111284411228441132844114284411528441162817441118

    字符串用作SqlParameter:

    mySqlCommand.Parameters.Add(new SqlParameter("@p_Ids", p_strValores));
    

    供下列资源使用( 作为资源添加 )在in运算符中查询:

    UPDATE tbl_Datos 
    SET col_Leyenda = (select col_Id from tbl_Leyenda where col_Leyenda = 'T') 
    WHERE col_Id in (@p_Ids)
    

    查询和in运算符的结果应该如下所示:

    UPDATE tbl_Datos 
    SET col_Leyenda = (select col_Id from tbl_Leyenda where col_Leyenda = 'T') 
    WHERE col_Id in (2844099,2844100,2844101,2844102,2844103,2844104,2844105,2844106,2844107,2844108,2844109,2844110,2844111,2844112,2844113,2844114,2844115,2844116,2844117,2844118)
    

    但它说它不能将nvarchar转换成int,我如何格式化要在(…ids…)中使用的参数

    如果我将参数p_strids与string.format(querystring,p_strids)一起使用,它就会工作,如下所示:

    queryString = UPDATE tbl_Datos SET col_Leyenda = (select col_Id from tbl_Leyenda where col_Leyenda = 'T') WHERE col_Id in ({0})
    strSql = string.Format(queryString, p_strValores)
    mySqlCommand = new SqlCommand(strSql, m_obSqlConnection);
    

    对于如何在第一种方法中将sql语句作为资源进行处理,有什么想法吗?

    谢谢

    3 回复  |  直到 7 年前
        1
  •  1
  •   Edward    7 年前

    tbl_datos上的col_id列或tbl_leyenda表中的col_leyenda被声明为数据类型nvarchar。该列中可能有至少包含一些非数字字符的数据。

    当sql尝试运行where语句时: WHERE col_Id in (@Ids)

    它无法将col_id中的非数字数据转换为@ids中的数据列表,它假定这些数据是整数。

    您可以通过在列表中的每个id周围加上单引号来解决这个问题。它看起来更像这样:

    '2844099','2844100','2844101','2844102','2844103','2844104','2844105','2844106','2844107','2844108','2844109','2844110','2844111','2844112','2844113','2844114','2844115','2844116','2844117','2844118'
    

    也可能是变量@p_leyenda作为整数值传入。你也应该试着把它变成一根绳子。类似于你上面的上校名单。

        2
  •  0
  •   Mallikh Kalkere    7 年前

    你需要的是一个分割函数。

    创建拆分函数,如图所示 here

    CREATE FUNCTION [dbo].[SplitString]
    (
        @sString nvarchar(2048),
        @cDelimiter nchar(1)
    )
    RETURNS @tParts TABLE ( part nvarchar(2048) )
    AS
    BEGIN
        if @sString is null return
        declare @iStart int,
                @iPos int
        if substring( @sString, 1, 1 ) = @cDelimiter 
        begin
            set @iStart = 2
            insert into @tParts
            values( null )
        end
        else 
            set @iStart = 1
        while 1=1
        begin
            set @iPos = charindex( @cDelimiter, @sString, @iStart )
            if @iPos = 0
                set @iPos = len( @sString )+1
            if @iPos - @iStart > 0          
                insert into @tParts
                values  ( substring( @sString, @iStart, @iPos-@iStart ))
            else
                insert into @tParts
                values( null )
            set @iStart = @iPos+1
            if @iStart > len( @sString ) 
                break
        end
        RETURN
    
    END
    

    然后按如下方式更改查询:

    UPDATE tbl_Datos 
    SET col_Leyenda = (select col_Id from tbl_Leyenda where col_Leyenda = 'T') 
    WHERE col_Id in (Select part from SplitString(@p_Leyenda,','))
    
        3
  •  0
  •   Rarm    7 年前

    最后,唯一的方法是将字符串转换为列表:

    List<string> p_Ids= p_strValores.Split(',').ToList();
    

    然后将列表中的每个值转换为int,并将它们添加到aux列表中,例如aux_p_id,然后在sql查询中使用它:

    UPDATE tbl_Datos SET col_Leyenda = (select col_Id from tbl_Leyenda where col_Leyenda = @p_Leyenda) WHERE col_Id in aux_p_Ids