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

  • SEARCH
  • Home
  • 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

using a c# asp.net page, building a SQL query string to run against an
I am building a SQL query that dynamically changes based on passed in $_GET
Im having problems building a query with the linq to sql data query expression
In sql 2005, instead of building a query from dateparts year, month and date,
We are dynamically building some SQL statements and we are utilizing the IN operator.
I am building a C#/ASP.NET app with an SQL backend. I am on deadline
We are building a new application in .net 3.5 with SQL server database. The
I'm building a simple ASP.NET web application in VS 2008 with a SQL 2005
I am trying to build a .NET web application using SQL to query AS400
I'm building an ASP.NET MVC 2 site that uses LINQ to SQL. In one

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.