473,725 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL query... is there a better way?

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

Nov 19 '05 #1
9 1123
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

"Scott Reynolds" wrote:
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

Nov 19 '05 #2
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.

"Scott Reynolds" <sc***@it-xenius.co.uk> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
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

Nov 19 '05 #3
Hi!

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

Scott
"Tu-Thach" <Tu*****@discus sions.microsoft .com> wrote in message
news:8F******** *************** ***********@mic rosoft.com...
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

"Scott Reynolds" wrote:

Nov 19 '05 #4
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[i]);
command.Paramet ers.Add(param);
...
end foor loop
execute your command to retrieve result.
"Scott Reynolds" wrote:
Hi!

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

Scott
"Tu-Thach" <Tu*****@discus sions.microsoft .com> wrote in message
news:8F******** *************** ***********@mic rosoft.com...
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

"Scott Reynolds" wrote:


Nov 19 '05 #5
Thank you Kevin! I'll turn Option Strict On.

Scott

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:u$******** ******@TK2MSFTN GP09.phx.gbl...
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.

"Scott Reynolds" <sc***@it-xenius.co.uk> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
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

Nov 19 '05 #6
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" <Tu*****@discus sions.microsoft .com> wrote in message
news:5C******** *************** ***********@mic rosoft.com...
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[i]);
command.Paramet ers.Add(param);
...
end foor loop
execute your command to retrieve result.
"Scott Reynolds" wrote:
Hi!

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

Scott
"Tu-Thach" <Tu*****@discus sions.microsoft .com> wrote in message
news:8F******** *************** ***********@mic rosoft.com...
> 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
>
> "Scott Reynolds" wrote:


Nov 19 '05 #7
Kevin,

What if I store a ListItem in ArrayList? Is this OK?

Scott
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.

"Scott Reynolds" <sc***@it-xenius.co.uk> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
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


Nov 19 '05 #8
> What if I store a ListItem in ArrayList? Is this OK?

Not if you want your app to perform as best it can. It's really easy to
create a custom strongly-typed Collection. Here's a link to an article that
explains how:

http://msdn.microsoft.com/library/de...ctionclass.asp

--
HTH,

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

"Scott Reynolds" <sc***@it-xenius.co.uk> wrote in message
news:ea******** ******@TK2MSFTN GP11.phx.gbl...
Kevin,

What if I store a ListItem in ArrayList? Is this OK?

Scott
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.

"Scott Reynolds" <sc***@it-xenius.co.uk> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
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



Nov 19 '05 #9
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" wrote:
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" <Tu*****@discus sions.microsoft .com> wrote in message
news:5C******** *************** ***********@mic rosoft.com...
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[i]);
command.Paramet ers.Add(param);
...
end foor loop
execute your command to retrieve result.
"Scott Reynolds" wrote:
Hi!

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

Scott
"Tu-Thach" <Tu*****@discus sions.microsoft .com> wrote in message
news:8F******** *************** ***********@mic rosoft.com...
> 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
>
> "Scott Reynolds" wrote:


Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules" for when and how to use single quotes and double quotes in ASP? thanks! ---------------------- SQL = SQL & "WHERE '" & REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
0
1250
by: John Macon | last post by:
------=_NextPart_000_04B4_01C36308.415CE100 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi all, Long time reader, first time poster, I hope that I get this right. I am setting up a database that reflects a relationship between two =
9
3134
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
14
9291
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per year and there could be multiple sales measures every year per client. There is another field called last update date. If there are multiple sales measures then need to select the one that's been entered last based on this field. Also, if there
10
3739
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty well and used a query plan that made sense. Now, I know what you're all thinking... stored procedures have to optimize for variable parameters, etc. Here's what I've tried to fix the issue: 1. Recompiled the stored procedure 2. Created a new,...
4
2863
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table that links to a transaction items table that links to the products table: (User Table) UserID Other user data
22
31205
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for June, and will return all records in that month.
11
16328
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction. We have been using oracle client 10.1.0.2 with it's odbc for a while without problem. The problem arose when we decided to reconnect all the tables and save password. Some query became suddenly very slow. Then I've discovered that the tables...
2
5597
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT cal_date, holiday_ID from Calendar, holiday_tbl WHERE (((calendar.cal_Date) Between . And .)) And Email_sent=0 AND Staff_ID=" & Staff_ID This works fine.
3
2311
by: bob laughland | last post by:
Hi All, I have a SQL query like this (I have tried to break the problem down to simplify it), select rowid from table where name in ('a', 'b', 'd') group by rowid Here is an example of data in the table rowid name
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.