473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Extract parameters from SQL statement

Hei,
I'm currently trying to write a program in C# that will allow users to
parametrize their queries.
For instance, I have a query like this:

SELECT * FROM Customers Where Region = @Region AND Gender > @Gender

How can I extract the Parameters names without using String
manipulation (which is not perfect since sql statements can also
contain '@' in LIKE clauses for example)

I tried this way
Dim comm As New OleDbCommand(SqlStatement, myCon)
comm.Prepare()
MsgBox(comm.Parameters.Count.ToString())
comm.Dispose()
myCon.Close()
but this always return me 0 (zero)

Do you have any idea on how to solve this generic problem?
Regars
Philippe Graca
Jul 20 '05 #1
3 3801
[posted and mailed, please reply in news]

Phil (ph************@hotmail.com) writes:
I'm currently trying to write a program in C# that will allow users to
parametrize their queries.
For instance, I have a query like this:

SELECT * FROM Customers Where Region = @Region AND Gender > @Gender

How can I extract the Parameters names without using String
manipulation (which is not perfect since sql statements can also
contain '@' in LIKE clauses for example)

I tried this way
Dim comm As New OleDbCommand(SqlStatement, myCon)
comm.Prepare()
MsgBox(comm.Parameters.Count.ToString())
comm.Dispose()
myCon.Close()
but this always return me 0 (zero)


C#? That looks like Visual Basic to me. :-?

IF I understand you correctly, you have a query string like:

SELECT * FROM tbl WHERE @a LIKE '@abc%' AND @bulle = 98

And you want to find @a and @bulle but not @abc? There is no support for
this in ADO .Net, but you would have to parse that string yourself. While
T-SQL is very quirky language to parse completely, this particular case
is not that rough: Traverse the string character by character (which
should be a lot easier in C# I guess, and when you encounter any @, ', "
and /* and -- you have a state change. If you see a ', you are blind to
everything but a new ', same goes for ". If you see /*, you look for */.
And if you see @, you continue until till you see a character that is
not legal in an identifier.

There is still a tricky part or two. Your parser must not misbehave if
fed illegal SQL. And how to handle of the users have entered a variable
in a place where a variable is not permitted, for instance in for a
table name. And most of all: how do you know the data type of the variable?

I think it is somewhere you need to sit down and reconsider whether it
is actually worth the effort.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Hi Erland & thx
1-> Yes, this is vb.net ;)
2-> I've thought of this kind of state parsing mechanism but this
cames to me as a not 100% reliable solution
I was hoping that SQL server had some kind of API (if ado.net doesn't)
so that I could identify the variables in a TSQL command. Hence, I
think I'm going to give up for the moment.
Thx again
Phil

Erland Sommarskog <es****@sommarskog.se> wrote in message news:<Xn**********************@127.0.0.1>...
[posted and mailed, please reply in news]

Phil (ph************@hotmail.com) writes:
I'm currently trying to write a program in C# that will allow users to
parametrize their queries.
For instance, I have a query like this:

SELECT * FROM Customers Where Region = @Region AND Gender > @Gender

How can I extract the Parameters names without using String
manipulation (which is not perfect since sql statements can also
contain '@' in LIKE clauses for example)

I tried this way
Dim comm As New OleDbCommand(SqlStatement, myCon)
comm.Prepare()
MsgBox(comm.Parameters.Count.ToString())
comm.Dispose()
myCon.Close()
but this always return me 0 (zero)


C#? That looks like Visual Basic to me. :-?

IF I understand you correctly, you have a query string like:

SELECT * FROM tbl WHERE @a LIKE '@abc%' AND @bulle = 98

And you want to find @a and @bulle but not @abc? There is no support for
this in ADO .Net, but you would have to parse that string yourself. While
T-SQL is very quirky language to parse completely, this particular case
is not that rough: Traverse the string character by character (which
should be a lot easier in C# I guess, and when you encounter any @, ', "
and /* and -- you have a state change. If you see a ', you are blind to
everything but a new ', same goes for ". If you see /*, you look for */.
And if you see @, you continue until till you see a character that is
not legal in an identifier.

There is still a tricky part or two. Your parser must not misbehave if
fed illegal SQL. And how to handle of the users have entered a variable
in a place where a variable is not permitted, for instance in for a
table name. And most of all: how do you know the data type of the variable?

I think it is somewhere you need to sit down and reconsider whether it
is actually worth the effort.

Jul 20 '05 #3
Like Erland said, you'll need to parse the SQL statement string if you let
users enter freeform text, . Another technique I've seen is to include SQL
expression builder functionality in an app (e.g. column name/operator/value)
so that you can parameterize the statement without parsing.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Phil" <ph************@hotmail.com> wrote in message
news:78**************************@posting.google.c om...
Hi Erland & thx
1-> Yes, this is vb.net ;)
2-> I've thought of this kind of state parsing mechanism but this
cames to me as a not 100% reliable solution
I was hoping that SQL server had some kind of API (if ado.net doesn't)
so that I could identify the variables in a TSQL command. Hence, I
think I'm going to give up for the moment.
Thx again
Phil

Erland Sommarskog <es****@sommarskog.se> wrote in message
news:<Xn**********************@127.0.0.1>...
[posted and mailed, please reply in news]

Phil (ph************@hotmail.com) writes:
> I'm currently trying to write a program in C# that will allow users to
> parametrize their queries.
> For instance, I have a query like this:
>
> SELECT * FROM Customers Where Region = @Region AND Gender > @Gender
>
> How can I extract the Parameters names without using String
> manipulation (which is not perfect since sql statements can also
> contain '@' in LIKE clauses for example)
>
> I tried this way
> Dim comm As New OleDbCommand(SqlStatement, myCon)
> comm.Prepare()
> MsgBox(comm.Parameters.Count.ToString())
> comm.Dispose()
> myCon.Close()
> but this always return me 0 (zero)


C#? That looks like Visual Basic to me. :-?

IF I understand you correctly, you have a query string like:

SELECT * FROM tbl WHERE @a LIKE '@abc%' AND @bulle = 98

And you want to find @a and @bulle but not @abc? There is no support for
this in ADO .Net, but you would have to parse that string yourself. While
T-SQL is very quirky language to parse completely, this particular case
is not that rough: Traverse the string character by character (which
should be a lot easier in C# I guess, and when you encounter any @, ', "
and /* and -- you have a state change. If you see a ', you are blind to
everything but a new ', same goes for ". If you see /*, you look for */.
And if you see @, you continue until till you see a character that is
not legal in an identifier.

There is still a tricky part or two. Your parser must not misbehave if
fed illegal SQL. And how to handle of the users have entered a variable
in a place where a variable is not permitted, for instance in for a
table name. And most of all: how do you know the data type of the
variable?

I think it is somewhere you need to sit down and reconsider whether it
is actually worth the effort.

Jul 20 '05 #4

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

Similar topics

4
by: rdraider | last post by:
Is there a function that will extract part of a string when the data you want does not occur in a specific position? Field "REF" is varchar(80) and contains an email subject line and the email...
3
by: Zlatko Matić | last post by:
Hello. I know how to call a parameterized stored procedure by using ADODB command object and parameters, but how can I execute the same query using adCmdText instead of adCmdStoredProc? Namely...
10
by: Robert Schultz | last post by:
I have a C/C++ file that I simply want to 'extract' a function from. Something like: extract <function name> <c or cpp file> I want it to return from the beginning of the function, to the end. ...
2
by: Mark | last post by:
Hi all, a quick ADO.NET question in regards to the command object. What are the advantages (if any) of specifying command parameters when executing a stored procedure over just calling the stored...
6
by: Mohammad-Reza | last post by:
Hi I want to extract icon of an exe file and want to know how. I look at the MSDN and find out that I can use ExtractIconEx() Windows API but in there are some changes to that api in c# I made...
6
by: Ben | last post by:
Hi We have a Dataset that has been populated from the output parameter of a Stored Procedure (@Output). I understand that I can extract a single item when the dataset is populated by a table...
17
by: Umesh | last post by:
Can anyone do it? ARMY1987- what say?
10
by: Pavel Shved | last post by:
Is there a program of wide use that automatically extracts template parameters requirements from source code of template functions? I mean the following: assume we have a template function that...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which are then placed on a server, but I need to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.