0

I'm having a problem in retrieving declared parameters in SQL query in C#.

Here is my SQL query in SQL server.

Declare @param1 int =1
Declare @param2 varhar(255)='Admin'

SELECT * FROM tblUsers WHERE role=@param2 AND id=@param1`

The query is executing successfully on SQL server.

I'd like to use C# to get all declared parameters and Data types. In this case:

PARAMETER    DATA TYPE
@param1      int
@param2.     Varchar

Finally, I'd like to assign the values of the parameters dynamically using a textbox.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kamweti C
  • 19
  • 6
  • 6
    Your T-SQL batch has local variables, not parameters. It would be necessary to parse the batch text to get the data types. It would be much easier if you encapsulate the T-SQL code in a stored procedure so the parameter names and types are easily discoverable. See [this](https://stackoverflow.com/questions/3038364/get-stored-procedure-parameters-by-either-c-sharp-or-sql). – Dan Guzman Oct 24 '21 at 10:45
  • 2
    Seems like a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You have a batch that uses local variables which are set using constants. You might think of them as "parameters" but they are not. I think you just need to parameterize the sql statement in your application. – SMor Oct 24 '21 at 12:07

0 Answers0