Home > Software engineering >  Loop through values in a string to pass to SQL
Loop through values in a string to pass to SQL

Time:08-24

I have a string that is being passed from a web form as:

ABC_12345;DEF_78901

how can I get the prefixes of each value and then combine those and pass such as to SQL 'ABC','DEF'

CodePudding user response:

First Split the string into Array

 Dim DBSplit1() As String
 DBSplit1 = YourString.Split(";")

 'Then get the the Prefix of you string

  Dim Result As New List(Of String)
    For Each item As String In DBSplit1
        Dim rs As String = item.Substring(0, 3)
        Result.Add(rs)
  Next

  'Then join the list of string with comma
  Dim StringResult As String = String.Join(", ", Result.ToArray())

CodePudding user response:

@John that's because I've been working on it.

Here is what I have so far and almost have it but I'm missing something and not sure what right now:

dim tmp as String()
dim capturedValue as String = ""

// these values are being passed in ABC_12345, DEF_78901
for each p as string in tmp
  capturedValue  = p.substring(0,3) & ","
next 
 

I need to have the capturedValue as 'ABC','DEF' which I have 'ABC,DEF,' right now

now, there can be 1 value passed in or 100 values passed in, there is no set number of what can be passed in

CodePudding user response:

I would recommend looking at this. To combine them, you could include them in your query while inserting them like this:

Dim Query As String
            Query = "INSERT INTO `schema`.`table` (column_name) values ('" & first & last &"');"
  • Related