I'm trying to find a proper way to do this instead of simple string concatenation.
I have a (simplified) SQL statement as follows:
SELECT * FROM Zones WHERE id in (@zoneIDs);
I plan to insert an @zoneIDs
value like such: 100000,100001,100002
I'm using VB in this particular instance, but it should apply to any .Net language using the MySQL connector. What I'm trying is this:
Private GetZones As String = "SELECT * FROM Zones WHERE id in (@zoneIDs)"
Dim Param As MySqlParameter = New MySqlParameter("@zoneIDs", MySqlDbType.String)
Param.Value = "100000,100001,100002"
Dim adapter As MySqlDataAdapter = New MySqlDataAdapter(GetZones)
Dim dt as DataTable = New DataTable()
adapter.Fill(dt)
But what's getting filled in dt
is just the row for ID 100000, as if none of the others were in the query. My guess is that it's running a command like this:
SELECT * FROM Zones WHERE id in ("100000,100001,100002");
Instead of
SELECT * FROM Zones WHERE id in (100000,100001,100002);
I personally prefer to use Stored Procedures instead of strings like this, but I believe I would hit a similar wall there. What is the proper way to fill an IN clause using parameters like this?