0

我有字符串数组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 条件下的错误

错误 101 索引数小于索引的维数

我怎样才能在这个查询中有字符串数组?

4

3 回答 3

2

例如,您可以使用以下Join方法String

String.Join(", ", GTIDArr)

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

"'" & String.Join("', '", GTIDArr) & "'"
于 2013-11-08T07:42:10.593 回答
1

您不能直接在此 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  "
于 2013-11-08T08:51:52.387 回答
1

哟可以通过使用 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

希望这会帮助你。

于 2013-11-08T07:55:21.867 回答