1

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!

egrunin
  • 24,650
  • 8
  • 50
  • 93
rd_rscan
  • 192
  • 3
  • 15
  • How are the SQL strings being generated? Are they being built up from code or are they coming from your users? – Conrad Frix Jan 27 '12 at 18:43
  • They were written without a place for parameter name's store... :-/ They came from users, and my app don't know the query parameters. – rd_rscan Jan 27 '12 at 18:55
  • In delphi I had something like FetchParams (on ClientDataSet) when its engine parse sql and do this. But in C# I not found any like it. – rd_rscan Jan 27 '12 at 19:07
  • So the reason why I asked is because you'll need to either parse it yourself, or getting a third party library. If you can control the way the SQL is constructed it would make parsing it yourself a little easier. I suspect that why parameters on data.stackexchange.com look like ##Param1## – Conrad Frix Jan 27 '12 at 19:12

1 Answers1

2

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

egrunin
  • 24,650
  • 8
  • 50
  • 93