Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to automatically get a list of parameters defined in a query string using SqlClient

I am working on a C# project that takes parameterized queries and saves them – then executes them on a schedule and emails out results (if any records are returned).

I am using the System.Data.SqlClient namespace to create SqlConnection, SqlCommand to contain the query and parameter information, and SqlDataReader to get the results of the query from the SqlCommand into something I can work with.

I have routines in place that "crawl" the supplied query to find any parameters in the string text (looking for words that start with "@") and save those as well – and it works fine – but I was wondering if there is something in the SqlClient library that will take the query text and output a list of found parameters?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

After researching the question, I haven’t come up with anything that says you can get a list of parameters defined in the query text by using "this thing".

It may not be possible, and this is simply me wondering if it is.
Currently, after getting a list of parameters by crawling the query string, I have to ask the user to define the datatypes – and that could go away if it’s possible to get a list automatically.

If you do not add a parameter to the SqlCommand that exists in the query string – you will get an exception saying something like

@ParameterName is defined in the query but no value was given

which is what makes me think it may be possible.

>Solution :

Not in SqlClient, as it has no data type information about the parameters. But SQL Server can infer parameter types from the column comparison expressions they are used in.

So check out the system stored procedure sp_describe_undeclared_parameters to give you this information.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading