|
 |
asp_databases thread: Query from SQL database
Message #1 by "Ken Buska" <kbuska@a...> on Mon, 12 Aug 2002 14:23:40
|
|
Hey all, I'm trying to query a sql database and I think i'm getting my
code mixed up. I want a page to send a variable and then take the
contents of that variable and query the SQL database and pull the proper
RS so that I may display its cells. Could you please take a look at the
code below and let me know what you think.
Thanks,
Ken Buska
kbuska@a...
<% Option Explicit %>
<!-- #include file="DataStore.asp" -->
<!-- METADATA Type="typeLib"
File="C:\PROGRAM FILES\COMMON
FILES\SYSTEM\ADO\MSADO15.DLL" -->
<%
'***************************************************
'The ImageRef Variable contains a name of a picture.
dim strImageRef
strImageRef = Request.Form("ImageRef")
dim objCommand, objRS
Set objCommand = Server.CreateObject("ADODB.Command")
objCommand.ActiveConnection = strConnect
objCommand.CommandText = "SELECT " & strImageRef & " FROM Image"
objCommand.CommandText = "WHERE Image
Like 'Buska_SBVisit_0802003.jpg'"
objCommand.CommandType = adCmdText
Set objRS = objCommand.Execute
Set objCommand = Nothing
Response.Write objRS("Location")
objRS.Close
Set objRS = Nothing
%>
Message #2 by "Gillian Harber" <gillian@m...> on Mon, 12 Aug 2002 14:19:15 +0100
|
|
> objCommand.CommandText = "SELECT " & strImageRef & " FROM Image"
> objCommand.CommandText = "WHERE Image
> Like 'Buska_SBVisit_0802003.jpg'"
I think the above lines are the problem.
I would have done:
sql = "SELECT " & strImageRef & " FROM Image"
sql = sql & "WHERE Image Like 'Buska_SBVisit_0802003.jpg'"
objCommand.CommandText = sql
But I'm sure someone else will give you a better way.
Gillian
Message #3 by "Gavin Landon" <glandon@g...> on Mon, 12 Aug 2002 15:54:15 -0500
|
|
Your LIKE statement needs a '%'. You don't use LIKE if you know exacly
what the field value is.
WHERE Image LIKE '%Image.jpg'
WHERE Image LIKE 'My%'
WHERE Image = 'MyImage.jpg'
"Gillian Harber" <gillian@m...> wrote in message
news:203633@a..._databases...
>
> > objCommand.CommandText = "SELECT " & strImageRef & " FROM Image"
> > objCommand.CommandText = "WHERE Image
> > Like 'Buska_SBVisit_0802003.jpg'"
>
> I think the above lines are the problem.
>
> I would have done:
> sql = "SELECT " & strImageRef & " FROM Image"
> sql = sql & "WHERE Image Like 'Buska_SBVisit_0802003.jpg'"
> objCommand.CommandText = sql
>
> But I'm sure someone else will give you a better way.
>
> Gillian
>
>
>
>
Message #4 by "Vaishnavi" <mail2vaish@y...> on Tue, 13 Aug 2002 06:17:32
|
|
Hi,
The problem is mainly this
objCommand.CommandText = "SELECT " & strImageRef & " FROM Image"
objCommand.CommandText = "WHERE Image Like 'Buska_SBVisit_0802003.jpg'"
objCommand.CommandText is set to two values, instead of appending the
string
Hence finally the execute would execute only "WHERE Image
Like 'Buska_SBVisit_0802003.jpg'"
If the string is properly appended and then set to command text it would
work but adding % in the end for a 'like' statemet would give all records
which start with the given value.
However if you are sure what value will be there in the database, it is
better to use = instead of like
Of course, this is better in terms of performance etc and otherwise 'like'
should be fine.
> Hey all, I'm trying to query a sql database and I think i'm getting my
c> ode mixed up. I want a page to send a variable and then take the
c> ontents of that variable and query the SQL database and pull the proper
R> S so that I may display its cells. Could you please take a look at the
c> ode below and let me know what you think.
> Thanks,
K> en Buska
k> buska@a...
> <% Option Explicit %>
<> !-- #include file="DataStore.asp" -->
<> !-- METADATA Type="typeLib"
> File="C:\PROGRAM FILES\COMMON
F> ILES\SYSTEM\ADO\MSADO15.DLL" -->
<> %
> '***************************************************
> 'The ImageRef Variable contains a name of a picture.
> dim strImageRef
>
> strImageRef = Request.Form("ImageRef")
> dim objCommand, objRS
> Set objCommand = Server.CreateObject("ADODB.Command")
> objCommand.ActiveConnection = strConnect
> objCommand.CommandText = "SELECT " & strImageRef & " FROM Image"
> objCommand.CommandText = "WHERE Image
L> ike 'Buska_SBVisit_0802003.jpg'"
> objCommand.CommandType = adCmdText
> Set objRS = objCommand.Execute
> Set objCommand = Nothing
> Response.Write objRS("Location")
>
> objRS.Close
> Set objRS = Nothing
%
|
|
 |