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

后端 未结 8 1079
慢半拍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:52

    Mark has the best implementation of DeriveParameters. As he said, make sure you cache like in this tutorial.

    However, I think this is a dangerous way of solving your original problem of database sproc versioning. If you are going to change the signature of a procedure by adding or removing parameters, you should do one of the following:

    • Code in a backwards-compatible manner by using defaults (for new params) or by simply ignoring a param (for deleted params). This ensures that your client code can always call any version of your stored procedure.
    • Explicitly version the procedure by name (so you will have my_proc and my_proc_v2). This ensures that your client code and sprocs stay in sync.

    Relying on DeriveParameters to validate what version of the sproc you're using seems like the wrong tool for the job, IMHO.

    0 讨论(0)
  • 2020-11-28 12:56

    You can use SqlCommandBuilder.DeriveParameters() (see SqlCommandBuilder.DeriveParameters - Get Parameter Information for a Stored Procedure - ADO.NET Tutorials) or there's this way which isn't as elegant.

    0 讨论(0)
提交回复
热议问题