I am working on a visual basic project. I have a mdb database connected to my project. I want to add a SELECT
query that finds the results which are in array that i give it on my program
I have tried to write a statement like that:
SELECT kodu, adi_soyadi, sectigi_ders_say FROM ogrenciler WHERE kodu IN ?
But it does not work. In my page codes I have an array and I want to find results from "ogrenciler"
table where the "kodu"
is in my array.
Advertisement
Answer
Well, you could send that array to a temp table in Access, but that would prevent more then one user using the software at the same time. (or you could add some user name to the temp table. However, if the array of choices is small, say about max 50, then you can create the sql string.
eg:
Dim MySQL As String = "SELECT * from tblHotels WHERE ID IN(" Dim IdList(5) As Integer Dim i As Integer For i = 1 To 5 IdList(i) = i Next Dim MyList As String = "" For i = 1 To 5 If MyList <> "" Then MyList = MyList & "," MyList = MyList & IdList(i) Next MySQL = MySQL & MyList & ")" Using MyCon2 As New OleDbConnection(My.Settings.OLESQL) Dim da As New OleDbDataAdapter(MySQL, MyCon2) Dim rstDat As New DataTable() da.Fill(rstDat) For i = 0 To rstDat.Rows.Count - 1 Debug.Print(rstDat.Rows(i).Item("HotelName")) Next ' etc etc. etc. End Using
So you can use the SQL format of:
SELECT * FROM tblHotels where ID IN (1,2,3)
And thus build up the “list”. The only downside to this approach is that the sql string is limited to 2000 characters. So, if your list is larger then say 50 or so items, then you have to adopt a different approach.