comma separated an array to query sql

1

I am using an array in VB.NET, then I pass it to a sql selection query in the following way

SELECT * FROM empresa WHERE factura IN ('" & val & "')

where val is an array, but the drawback is that each value of the array must be separated by commas to be able to use it in the previous query. How can I pass the array separated by commas? This is the way I keep my array

Dim array() As String = {factura}

For Each value As String In array
    Dim val
    If Not String.IsNullOrEmpty(value) Then
     val = value + ","
    Else
    val = value

    End If
    consultaEnvio = db.Query("SELECT * FROM empresa WHERE             factura IN ('" & val & "')")

    For Each A In consultaEnvio
    MsgBox(A("NOMBRE")+A("FACTURA"))
    Next
Next
    
asked by Ivxn 01.07.2016 в 18:35
source

3 answers

3

You could use

Dim array() As String = {factura}

Dim filtroIN As String = String.Join(",", array)

consultaEnvio = db.Query("SELECT * FROM empresa WHERE factura IN (" & filtroIN & ")")

For Each A In consultaEnvio
    MsgBox(A("NOMBRE")+A("FACTURA"))
Next

with the String.Join () you could join an array with a separator

    
answered by 01.07.2016 / 19:11
source
0

Leandro has already answered you in an excellent way, I contribute with another example of the same type.

Dim sqlquery As String
Dim sqlquerys As New ArrayList()

'There you define first the "header" of the query and the array you will use.

sqlquery = "INSERT INTO FERIANTES(Nombre, Apellido) VALUES "
For x = 0 To ListaDeFeriantes.Count - 1
Dim CONSULTA As String = ""
CONSULTA = "('" & ListaDeFeriantes.Nombre & "," & ListaDeFeriantes.Apellido & ")"
sqlquerys.Add(CONSULTA)
Next
sqlquerys.ToArray()
Dim strCommand As String = [String].Join(",", sqlquerys.ToArray())

'And finally the consultation will unite with:

Dim ConsultaFinal as String = sqlquery & strCommand
    
answered by 06.07.2016 в 16:01
0

Once you have filled your arrangement you should go through it and create your chain that you will send within in

Dim cadenaIn As String =  ""
For Each value As String In array
    cadenaIn = cadenaIn & value & ","
next    
cadenaIn = left(cadenaIn,(Len(cadenaIn)-1))'aca eliminamos la coma del ultimo elemento
consultaEnvio = db.Query("SELECT * FROM empresa WHERE factura IN ('" & cadenaIn & "')")

For Each A In consultaEnvio
    MsgBox(A("NOMBRE")+A("FACTURA"))
Next
    
answered by 01.07.2016 в 19:12