How can I retrieve a list of parameters from a stored procedure in SQL Server

后端 未结 8 1123
慢半拍i
慢半拍i 2020-11-28 12:14

Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters that belong to a stored procedure on a SQL Server before I actually execute it?

I

8条回答
  •  星月不相逢
    2020-11-28 12:46

    You want the SqlCommandBuilder.DeriveParameters(SqlCommand) method. Note that it requires an additional round trip to the database, so it is a somewhat significant performance hit. You should consider caching the results.

    An example call:

    using (SqlConnection conn = new SqlConnection(CONNSTRING))
    using (SqlCommand cmd = new SqlCommand("StoredProc", conn)) {
       cmd.CommandType = CommandType.StoredProcedure;
       SqlCommandBuilder.DeriveParameters(cmd);
    
       cmd.Parameters["param1"].Value = "12345";
    
       // ....
    }
    

提交回复
热议问题