Datatable select with multiple conditions SQL Query issue
Asked Answered
F

1

8

From This question, its answer is almost my answer. But I am facing some sql query issue, I have the following statement in VB

Dim results As DataRow() = table.Select("A = 'foo' AND B = 'bar' AND C = 'baz'")

I want to place foo, bar and baz in variables and use that variables in above statements.

Dim Varfoo As String = "foo"
Dim Varbar As String = "bar"
Dim Varbaz As String = "baz"

I managed to get one variable in statement as

 Dim results As DataRow() = table.Select("A = " + Varfoo)

But how to insert multiple sort expressions with variables?

Edit: I got it solved with the answer of vikas as following;

Dim results As DataRow() = table.Select("A = '" & Varfoo & "' And B = '" & Varbar & "' And C = '" & Varbaz & "'")
Foregone answered 21/1, 2013 at 8:30 Comment(1)
You can refer to this link: have more explanation: csharp-examples.net/dataview-rowfilterPablopabon
R
15

Have you tried

Dim results As DataRow() = table.Select("A = '" & Varfoo & "'")

Edited

For OR operation

Dim results As DataRow() = table.Select("A = '" & Varfoo & "' OR B = '" & Varbar & "' OR C = '" & Varbaz & "'")

For AND operation

Dim results As DataRow() = table.Select("A = '" & Varfoo & "' AND B = '" & Varbar & "' AND C = '" & Varbaz & "'")
Roxana answered 21/1, 2013 at 8:38 Comment(3)
@Foregone did u try my logicRoxana
This also for the single sort expression as i mention Dim results As DataRow() = table.Select("A = " + Varfoo) which is working fine. But I want for multiple sort expressions using 'AND'.Foregone
Hey I got it working with your answer. Thanks a lot Man. Please see my edited question for how i solved it.Foregone

© 2022 - 2024 — McMap. All rights reserved.