从SqlDataSource的给定存储过程名称获取参数列表存储过程、名称、参数、列表

2023-09-05 00:15:26 作者:满城花祭泪﹌

有没有一种理智的方式来获得预期的参数列表,从给定的分配给其SelectCommand属性的存储过程的名称的SqlDataSource的?

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?

推荐答案

您可以使用SqlCommandBuilder.DeriveParameters方法返回与存储过程相关的参数信息。遍历集合将允许您确定参数名称,数据类型,方向等。

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;

看看的 SqlParameterCollection 和的SqlParameter 类定义的详细信息。

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