如何从 SQL Server 中的存储过程中检索参数列表 [英] How can I retrieve a list of parameters from a stored procedure in SQL Server

查看:23
本文介绍了如何从 SQL Server 中的存储过程中检索参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 C# 和 System.Data.SqlClient,有没有办法在我实际执行之前检索属于 SQL Server 上的存储过程的参数列表?

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 have an a "multi-environment" scenario where there are multiple versions of the same database schema. Examples of environments might be "Development", "Staging", & "Production". "Development" is going to have one version of the stored procedure and "Staging" is going to have another.

我要做的就是在传递一个值并调用存储过程之前验证一个参数是否存在.避免 SqlException 而不必捕获它对我来说是一个加分项.

All I want to do is validate that a parameter is going to be there before passing it a value and calling the stored procedure. Avoiding that SqlException rather than having to catch it is a plus for me.

约书亚

推荐答案

可以使用SqlCommandBuilder.DeriveParameters()(见SqlCommandBuilder.DeriveParameters - 获取存储过程的参数信息 - ADO.NET 教程)或者有 这种方式 不那么优雅.

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.

这篇关于如何从 SQL Server 中的存储过程中检索参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆