【问题标题】:including array of string into "where GTID in (arrayname)" clause将字符串数组包含在“where GTID in (arrayname)”子句中
【发布时间】:2013-11-08 07:37:22
【问题描述】:

我在GTIDArr 中有字符串数组

让我们说GTID={"CODE1","CODE2","CODE3"}

我想从具有上述 GTID 的以下查询的数据库中获取记录。

因此我做了以下事情:

Dim sqlTransferExcel As String = " select gt.GTID ,gt.TransferDate ,pl.LocationName as FromLocation,Pto.LocationName as ToLocation,gt.TransferedMode ,gt.TransferedDetails ,gt.Remarks ,p.ProductName+case when c.CategoryName IS null then '' else  '-'+c.CategoryName end as PName,gd.QtyTransferred  " _
                                      & " from dbo.GoodTransferDetails gd Left outer join ProductMaster p on gd.ProductID =p.ProductID " _
                                      & " Left Outer join CategoryMaster c on gd.CategoryID =c.CategoryID " _
                                      & " Left Outer join dbo.GoodTransfer gt on gt.GTID=gd.GTID  " _
                                      & " Left Outer join  PescaLocation pl on  gt.FromPescaLocation =pl.PLID " _
                                      & " Left Outer join PescaLocation Pto  on gt.ToPescaLocation =pto.PLID " _
                                      & " where gd.GTID in ( '" & GTIDArr() & "' ) order by p.OrderID  "

但它给了我在 where 条件为的最后一行的错误

Error 101 Number of indices is less than the dimension of the number of 被索引的

如何在此查询中包含字符串数组??

【问题讨论】:

    标签: c# asp.net .net sql vb.net


    【解决方案1】:

    例如,您可以使用String 中的Join 方法:

    String.Join(", ", GTIDArr)
    

    如果您需要引用数组中的每个元素,请尝试:

    "'" & String.Join("', '", GTIDArr) & "'"
    

    【讨论】:

    • 谢谢兄弟...很有帮助。
    【解决方案2】:

    你可以通过使用 LINQ 来做到这一点

    string Command= string.Empty;
     Command= string.Join(",", ArrayListName.Cast<string>().Select(x => x.ToString()).ToArray()); // If it is INT Type
     Command= string.Join("','", ArrayListName.Cast<string>().Select(x => x.ToString()).ToArray()); // If it is String
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案3】:

      你不能直接使用数组和这个sql语句中的&来在你的数组中使用&所以你首先必须转换为字符串然后你必须使用.see下面的代码,你很容易找到解决方案:

      Dim GTIDArr As String() = {"CODE1", "CODE2", "CODE3"}
      
      Dim value As String = String.Join(",", GTIDArr)
      
      Dim sqlTransferExcel As String = " select gt.GTID ,gt.TransferDate ,pl.LocationName as FromLocation,Pto.LocationName as ToLocation,gt.TransferedMode ,gt.TransferedDetails ,gt.Remarks ,p.ProductName+case when c.CategoryName IS null then '' else  '-'+c.CategoryName end as PName,gd.QtyTransferred  " _
        & " from dbo.GoodTransferDetails gd Left outer join ProductMaster p on gd.ProductID =p.ProductID " _
        & " Left Outer join CategoryMaster c on gd.CategoryID =c.CategoryID " _
        & " Left Outer join dbo.GoodTransfer gt on gt.GTID=gd.GTID  " _
        & " Left Outer join  PescaLocation pl on  gt.FromPescaLocation =pl.PLID " _
        & " Left Outer join PescaLocation Pto  on gt.ToPescaLocation =pto.PLID " _
        & " where gd.GTID in ( '" & value & "' ) order by p.OrderID  "
      

      【讨论】:

        猜你喜欢
        • 2022-08-18
        • 1970-01-01
        • 1970-01-01
        • 2018-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多