sql query to check values in array

Last post 08-03-2008 3:37 AM by closeguy2005. 2 replies.

Sort Posts:

  • sql query to check values in array

    08-03-2008, 2:45 AM
    Hi.. Dim names() As String is my array It holds 3 values I want to write Sql query to match any of the values of Name column matches with values in array names() and display result Please guide me about query and syntax Thank you
    Please Mark the post as "ANSWER" if it helps you
  • Re: sql query to check values in array

    08-03-2008, 2:55 AM
    • Star
      14,467 point Star
    • donkiely
    • Member since 06-18-2002, 6:28 PM
    • Fairbanks, Alaska
    • Posts 2,442
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Does the array always hold three values? If so, something like this will work:

    SELECT * FROM table WHERE Name = @Name1 OR Name = @Name2 OR Name = @Name3

    Use that statement to build a SqlCommand object, pass in the values of the array as th3 @Name1, @Name2, and @Name3 parameters, and make the call.

    Will that work for you? If not, provide more details.

    Don

    Don Kiely, MCP, MCSD
    In the Last Frontier, Interior Alaska
    Please post questions and replies to the forum! And remember to MARK AS ANSWER when someone definitively answers a question or resolves a problem!
  • Re: sql query to check values in array

    08-03-2008, 3:37 AM

    donkiely:

    Does the array always hold three values? If so, something like this will work:

    SELECT * FROM table WHERE Name = @Name1 OR Name = @Name2 OR Name = @Name3

    Use that statement to build a SqlCommand object, pass in the values of the array as th3 @Name1, @Name2, and @Name3 parameters, and make the call.

    Will that work for you? If not, provide more details.

    Don

     

    Thanks for the help.

    But my actual query is not i mentioned above...That I gave just to make problem simplified but i think i should provide

    details.

     
    here is my code: // I Have avoided to write un-necessary statements here

    SelectCommand="SELECT jobs.Job_title,Company,Location,Details,Code,Candidate,Minexp,Date FROM jobs where [Job_title]Like '%' +@Job_title+'%' and [Minexp]>=@Minexp "

     

    <SelectParameters>

            <asp:SessionParameter SessionField="txtsearch" Name="Job_title" />
            <asp:SessionParameter SessionField="txtmin" Name="Minexp" Type="Int32" />
           
    </SelectParameters>

     _____________________ _______________________________________________________________________

    it works fine......but I have one more session variable called Session("txtlocation") which holds multiple values     ( values selected from list box) . I am using string array in my page's load event to copy all values from
    Session("txtlocation")

    Dim str() As String = CType(Session("ListBoxValues"), String()) 

    I want to use Session("txtlocation") in  <asp:SessionParameter ......>  just like I used above session variables

    1 ] <asp:SessionParameter SessionField="txtlocation" Name="txtloc">    will be the right format ?

    2]  Also, i want to write in sql query to match location specified by user to any location which Session("txtlocation")
        holds . How will the query be ?

     where [Location] like '%' +@txtloc+'%'    //but then how to specify multiple values of location which Session("txtlocation") holds???
     

    _____________________ _______________________________________________________________________

    In short, this query not working..something wrong about array i used below..

    SelectCommand="SELECT ------ FROM ----- where [Location]Like '%' +@loc[0]+'%' "

     

     

    I have one solution in mind....Since there are fix number of values in list, i can copy them in string variable

    suppose we copy first value in str variable then the query will be? where [location] like ' "&str&"' ??

    is this syntax correct ??

     

    Please Mark the post as "ANSWER" if it helps you
Page 1 of 1 (3 items)