SqlCommand (CommandType Text) retrieve param collection

1.4k views Asked by At

Is there a way to get the parameter collection from SqlCommand when CommandType = Text?

For instance:

string MyDinamicSql = @"SELECT * FROM USERS where USERName = @Param1 and USERLogin=@Param2";
SqlCommand comand = connection.CreateCommand();
comand.CommandText = MyDinamicSql;
comand.CommandType = System.Data.CommandType.Text;

//Do something for fill comand.Parameters from MyDinamicSql...

I want a way to get a parameter collection with [@Param1,@Param2]... I don't know the sql queries (they are dynamic) and I want to get the parameters for my application to create them as input controls.

I can't do this with SqlCommandBuilder.DeriveParameters(command), because it only works with stored procedures.

Thank you!

1

There are 1 answers

2
egrunin On BEST ANSWER

No - until SQL Server parses the command, it's just a string.