Re: how to access final SQL from parameterized ADO
- From: "Bob Barrows [MVP]" <reb01501@xxxxxxxxxxxxxxx>
- Date: Tue, 12 Sep 2006 15:52:56 -0400
dondraper wrote:
Here's why: I want to save the complete query so a user can name it
and then run it again at a later date...
So give the set of parameter values a name and re-use them at a later
time ...
In a web application, my users use different means (query by
example form or report builder interface) to construct a query against
a database. The query is run and the user views a report of the
results. The user then has the option to "save" this report as a named
report.
Ahhh! Ad hoc queries! Thus far, I've been able to convince my users that
they don't need those.
How is the rest of the statement built? Via concatenation? is the user
allowed to supply the names of the tables or columns? If so, there's a
security risk. Even if you are restricting the words to be used via
dropdowns or lists, a hacker could conceivably cause his own "data" to
be submitted, so hopefullly you are validating these inputs pretty well
on the server.
If I could access the query sent to SQL Server, I could easily store
this away in the database and allow the user to later retrieve it,
then pass it to the cmd.CommandText and have ADO run the query again.
Very easy and efficient plus I know the query was constructed
correctly and will accurately return the original results.
However, using paramaterized queries (which we all should for better
datatype checking, security, performance, etc) now complicates things
as I cannot save a simple query generated by a user.
The original SQL
with my embedded question mark placeholders is useless without the
original values and datatypes.
Actually, given that vbscript consists of nothing but variants, you
really don't need to build a parameters collection. Let's look at your
initial example:
SQLstmt = "select col1, col2 from table1 where col3 = ?"
set dc = Server.CreateObject("ADODB.Connection")
set cmd = Server.CreateObject("ADODB.Command")
dc.ConnectionTimeout = 30
dc.CursorLocation = Application("myCursorLocation")
dc.Open Application("myConnectionString")
SET cmd.ActiveConnection = dc
cmd.CommandType = adCmdText
cmd.CommandText = SQLstmt
arParms = Array(21)
set rs=cmd.Execute(,arParms,1)
However, there are cases (usually involving dates) where it is necessary
to convert the values to the correct datatype before putting them in the
parameter array.
And we wonder why so many developers
continue to use concatenated strings to build their SQL statements. I
would bet this is one of them.
Maybe. I think the overriding reason is that this is the only technique
shown in samples, tutorials, etc. Which is a crime in my book.
Nothing builtin. You would have to write your own code to loop through
One final question...is it possible under ADO 2.8 to serialalize the
parameters collection so it could be stored and later retrieved? If
yes, would this retain the values added to those parameters? If yes,
any assistance in that direction would be appreciated.
the collection and serialize it (perhaps to an xml document), as well as
the code to deserialize it.
Of course, you could create the delimited strings (or xml document) as
you were building the parameters collection.
And of course, since you would be writing the code, you can choose to
store the values as well as the other parameter properties.
Here is a link to my stored procedure code generator:
http://common.mvps.org/barrowsb/ClassicASP_sp_code_generator.zip
Perhaps you can reverse-engineer it and get some ideas about creating a
serializer/deserializer.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
.
- References:
- how to access final SQL from parameterized ADO
- From: dondraper
- Re: how to access final SQL from parameterized ADO
- From: dondraper
- Re: how to access final SQL from parameterized ADO
- From: Bob Barrows [MVP]
- Re: how to access final SQL from parameterized ADO
- From: dondraper
- how to access final SQL from parameterized ADO
- Prev by Date: Re: how to access final SQL from parameterized ADO
- Next by Date: How to Connect my Database to my web page.(or web server)
- Previous by thread: Re: how to access final SQL from parameterized ADO
- Next by thread: Re: Difficulty installing MDAC 2.8 SP1 on Windows 2000
- Index(es):
Relevant Pages
|