Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a Parameter List from an SQLDataSource given Stored Procedure Name

Is there a sane way to get a list of expected parameters from an SqlDataSource given the name of the stored procedure assigned to its SelectCommand property?

like image 984
Pete Michaud Avatar asked Mar 11 '09 17:03

Pete Michaud


2 Answers

You can use the SqlCommandBuilder.DeriveParameters method to return information on the parameters associated with a stored procedure. Iterating over the collection will allow you to determine the parameter name, data type, direction, etc.

SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection("myConnectionString");
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "myStoredProcName";
cmd.Connection = conn;

conn.Open();
SqlCommandBuilder.DeriveParameters(cmd);
conn.Close();

SqlParameterCollection collection = cmd.Parameters;

Take a look at the SqlParameterCollection and SqlParameter class definitions for more information.

like image 54
sgriffinusa Avatar answered Nov 07 '22 15:11

sgriffinusa


I am not sure if you can do it in the ADO.NET environment directly, somehow - but you could always query the "sys" system views to get that information:

select * from sys.parameters
where object_id in 
      (select object_id from sys.procedures where name = 'YourSProc')

Marc

like image 34
marc_s Avatar answered Nov 07 '22 16:11

marc_s