PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Assigning Multiple Values To A Variable-VBForums
Results 1 to 5 of 5

Thread: Assigning Multiple Values To A Variable

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    459

    Assigning Multiple Values To A Variable

    In SQL, I can use code below to assign multiple values to a variable.

    select * from flower where color in ('red', 'yellow', 'green')

    How make this code working in vb.net like below?

    select * from flower where color in ("red", "yellow", "green")

    This is sentence will use to create a dataset which binding to a grid.
    In color table, there more than 10 colors were stored.
    Last edited by aspfun; Aug 9th, 2018 at 09:21 AM.

  2. #2
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    891

    Re: Assigning Multiple Values To A Variable

    Could you clarify what you are trying to achieve? Your question isn't really being very specific.

    Do you just want to store a list of values? If so your variable can be an Array or a List(Of ...) type and it can hold multiple values.

    Are you wanting to use the SQL like searching syntax? If so have a look at Linq.

    If it is something else then could you clarify your question please?

  3. #3
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    690

    Re: Assigning Multiple Values To A Variable

    Quote Originally Posted by aspfun View Post
    In SQL, I can use code below to assign multiple values to a variable.

    select * from flower where color in ('red', 'yellow', 'green')

    How make this code working in vb.net like below?

    select * from flower where color in ("red", "yellow", "green")

    This is sentence will use to create a dataset which binding to a grid.
    In color table, there more than 10 colors were stored.
    I would argue that you are not assigning multiple values to a variable, but supplying arguments, or params to a clause. If youre asking how to create this string variable it would look like this
    Code:
    Dim SQLString As String = "select * from flower where color in ('red', 'yellow', 'green')"

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,673

    Re: Assigning Multiple Values To A Variable

    Quote Originally Posted by kpmc View Post
    I would argue that you are not assigning multiple values to a variable
    There's no argument to be had. It's flat out not happening. There is no variable there at all.

    If the question here is how to construct a String containing a SQL SELECT statement with an arbitrary number of items in an IN clause then I would suggest checking out this one I prepared earlier:

    http://www.vbforums.com/showthread.p...-SQL-IN-Clause

    In the case that parameters are not required, i.e. the values are predefined and thus not susceptible to SQL injection or other issues, then you could do something like this:
    vb.net Code:
    1. Dim sql = String.Format("SELECT * FROM flower WHERE color IN ('{0}')",
    2.                         String.Join("', '",
    3.                                     colorListBox.SelectedItems.Cast(Of String)())
    One nifty thing about that specific syntax is that you'll end up with a single empty string inside the parentheses if there are no items selected.

    If the question actually relates to something other than building a SQL query then it needs to be restated in a much clearer form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    690

    Re: Assigning Multiple Values To A Variable

    There's no argument to be had. It's flat out not happening. There is no variable there at all.
    I was trying to be nice :/

    Thats a pretty clever example Jmc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width