Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:11:46+00:00 2026-05-23T07:11:46+00:00

I am building up an SQL query in VB.net and have written a routine

  • 0

I am building up an SQL query in VB.net and have written a routine to do it dynamically.

I want to include each item in an array (of unknown size) in the where clause, as such:

Dim person(10) as String
Dim strSQL, strWhereClause as String
person(0) = "John"
person(1) = "Steve"
'...

For i = 0 To UBound(menuNames)
    strWhereClause &= "[name] = '" & person(i) & "' OR "
Next

strSQL= "SELECT * FROM [customers] WHERE " & strWhereClause 

The problem here is that there is an extra " OR " at the end. Is there a neat way of removing this? Or perhaps a better way to approach the problem all together. (I think a string.join will not work in this situation, because there is text before and after the array item.)

EDIT: Manually removing the final three characters like so:strWhereClause = Left(strWhereClause, strWhereClause.Length - 3) is fine, except it doesn’t handle the empty string. After all, I don’t know how many elements will be in my array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:11:46+00:00Added an answer on May 23, 2026 at 7:11 am

    You should not do this in the way you do, even if you are 100% sure what contents are stored in the array it is still a bad habit. Instead you should get yourself familiar using parameterized queries.


    Not compile-tested:

    var sb = new StringBuilder();
    sb.Append("SELECT * FROM [customers] WHERE ");
    
    for (int i = 0; i < person.Length; ++i)
    {
        var param = string.Format("NAME{0}", i);
        sb.Append(string.Format("[NAME] = @{0}{1}", param, i < person.Length - 1 ? " OR " : string.Empty));
        command.Parameters.AddWithValue(param, person[i]);  
    }
    
    var sql = sb.ToString();
    

    Assuming you SqlCommand is command.
    Sorry for the C# but my VB is too rusty, i hope you get the point. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning .net and building a site that uses url routing. I have
I'm building an abstract gem. i need a sql query that looks like this
I have a PL/SQL procedure with multiple parameters. When a webapp calls the procedure,
I'm writing a little library to help building SQL requests (only doing SELECTs for
I'm using Lucene.net to build a MyListOfIds As List(Of Integer) which I then pass
I'm building a site that will link to various other sites using php scripts
I'm building a home rental system. The system stores profiles of homes and users
Do you know a way of implementing something like a Hibernate QBE (Query by
I recently redesigned my site, complete with a new database, but I wanted to
I am a complete newbie to Perl, with PHP coding experience and I put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.