SQL query... is there a better way?

S

Scott Reynolds

Hi,

I am using following code to read and generate SQL query based on values
stored in ArrayList. But I am not sure if it is the best way... all
suggestions are
welcome!

Thank you!
Scott


Dim SqlQuery As String
Dim SqlCity As String

If Not Cities.Count = 0 Then
For i As Integer = 0 To Cities.Count - 1
If i = 0 Then
SqlCity = "(City =" + Cities(i) + ")"
Else
SqlCity = SqlCity + " Or (City =" + Cities(i) + ")"
End If
Next
SqlCity = "(" & SqlCity & ")"
End If

SqlQuery = "Select * FROM MyTable WHERE " & SqlCity
 
G

Guest

It is better for you to use parameter instead of passing in the value
directly. You can achieve what you wanted with String.Join method though.

Tu-Thach
 
K

Kevin Spencer

Well, I do have one suggestion. First, TURN OPTION STRICT ON!!!!

It really would help you to learn about data types. If you can understand
data types, you can understand how data is stored, and what exactly it is.
This prevents a lot of errors, and increases your programming power
personally. It will also help your app run faster.

An ArrayList is a Collection of objects. As such, it is not typed data per
se (everything inherits from object). This means that you will either have
to cast the proper data type, or have Option Strict turned off in order to
compile your app. When Option Strict is turned off, and you use untyped
data, "Late Binding" occurs at run-time. This means that the .Net platform
has to query the data to find out what it is, which will slow down your
app's performance. In addition, as you can add ANY type of object to the
Collection, it is possible, for example, to assign a DataReader to one of
the values in your ArrayList, which would throw an exception at run-time,
although it would compile quite nicely. I have seen classes that defined
private fields as Integers, and exposed them as public string properties.
Not a problem, unless, of course, something tries to assign "foo" to the
value. "foo" is indeed a string, but it cannot be parsed into a number.
Hence, difficult-to-identify errors can pop up in your code at run-time.

The fastest enumerated type to use in this case would be an array of
strings. Of course, an array is of fixed length, so you need to either know
how many elements are in your array when you declare it, or you have to use
a Collection. You can certainly create a safely-typed Collection, by
inheriting CollectionBase in a class, and adding your own typed
implementations of the Add, Remove, etc. methods.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
G

Guest

For your case you might use this in your loop (C#, not VB):

SqlCommand command = new SqlCommand();
....
for loop..
....
SqlParameter param = new SqlParameter("@City" + i, Cities);
command.Parameters.Add(param);
...
end foor loop
execute your command to retrieve result.
 
S

Scott Reynolds

I don't know if your solution works for me, because I need to achieve
following...

SELECT * FROM Cities WHERE City = 'City1' OR City = 'City2' OR City =
'City4'

depends on what values are stored in array. What do you think?

Scott

Tu-Thach said:
For your case you might use this in your loop (C#, not VB):

SqlCommand command = new SqlCommand();
...
for loop..
....
SqlParameter param = new SqlParameter("@City" + i, Cities);
command.Parameters.Add(param);
...
end foor loop
execute your command to retrieve result.


Scott Reynolds said:
Hi!

Could you please provide me an example how to use parameter?

Scott
 
G

Guest

You still have to build the SQL string, but you should be building so that it
looks like

SELECT * FROM Cities WHERE City = @City1 OR City = @City2 OR City = @City4


Scott Reynolds said:
I don't know if your solution works for me, because I need to achieve
following...

SELECT * FROM Cities WHERE City = 'City1' OR City = 'City2' OR City =
'City4'

depends on what values are stored in array. What do you think?

Scott

Tu-Thach said:
For your case you might use this in your loop (C#, not VB):

SqlCommand command = new SqlCommand();
...
for loop..
....
SqlParameter param = new SqlParameter("@City" + i, Cities);
command.Parameters.Add(param);
...
end foor loop
execute your command to retrieve result.


Scott Reynolds said:
Hi!

Could you please provide me an example how to use parameter?

Scott


It is better for you to use parameter instead of passing in the value
directly. You can achieve what you wanted with String.Join method
though.

Tu-Thach

:

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top