5

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?

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

I dont have VS.NET to try it out, but .Prepare() may do this for you :)

Or you could just add the parameters. :)

Comments

0

And also with:

EXEC sp_sproc_columns 'YourSProc'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.