简体   繁体   中英

Vb.net How do i get a Dim statement between “”?

I am trying to get a Dim statement between "".

Dim TEST As String = "1" & "2" & "3"

SQL = "SELECT * from MYDATABASE where "this is were i want the dim statement" like '%" & search.Text & "%'"

I am tring to instert multiple tables inside it. How will this work?

I realise you've said Tables above, however, I suspect from your comments that you mean Columns not Tables, as you talking about querying the Last Name as well as the name?

Dim Columns As New List(Of String)

'Add as many columns as you like here...
Columns.Add("FirstName")
Columns.Add("LastName")
Columns.Add("Address")

'Initialise you Query String...
Dim SQL As String = "SELECT * FROM MyDatabase.MyTable WHERE "

'Loop through all the columns you want to search...
For Each Column As String In Columns

    'Check if this is the last item... If so, we don't need the trailing OR!
    If ReferenceEquals(Columns.Item(Columns.Count - 1), Column) Then

        SQL &= Column & " LIKE '%" & search.Text & "%'"

    Else

        SQL &= Column & " LIKE '%" & search.Text & "%' OR "

    End If

Next

'
'Go on and perform your query...
'

However, if you really are referring to different tables, then you're going to have to do some joining too..

Also, as mentioned in the comments, this is unsafe, and vulnerable to SQL injection... But it will work if you're in a high trust environment...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM