是否可以自动获取使用SQLClient在查询字符串中定义的参数列表

发布于 2025-02-13 01:14:55 字数 710 浏览 0 评论 0原文

我正在研究一个C#项目,该项目进行参数化查询并保存它们 - 然后按时间表执行并通过电子邮件发送结果(如果返回任何记录)。

我正在使用system.data.sqlclient命名空间来创建sqlConnectionsqlCommand用于包含查询和参数信息,以及sqldatareader sqldatareader /code>将查询结果从sqlcommand中获取我可以使用的东西。

我有一个例程“爬网”所提供的查询以查找字符串文本中的任何参数(寻找以“@”开头的单词)并保存这些参数 - 而且它可以正常工作 - 但我想知道是否有东西在sqlclient库中,将获取查询文本并输出找到参数的列表?

在研究了这个问题之后,我没有想出任何说明您可以通过使用“此东西”在查询文本中定义的参数列表的内容。

这可能是不可能的,这简直就是我想知道这是否。 当前,在通过爬网字符串获得参数列表后,我必须要求用户定义数据类型 - 如果可能自动获取列表,则可以消失。

如果您不在查询字符串中存在的sqlCommand中添加参数

@parametername是在查询中定义的,但没有给出任何值

这就是我认为这可能是可能的。

I am working on a C# project that takes parameterized queries and saves them - then executes them on a schedule and emails out results (if any records are returned).

I am using the System.Data.SqlClient namespace to create SqlConnection, SqlCommand to contain the query and parameter information, and SqlDataReader to get the results of the query from the SqlCommand into something I can work with.

I have routines in place that "crawl" the supplied query to find any parameters in the string text (looking for words that start with "@") and save those as well - and it works fine - but I was wondering if there is something in the SqlClient library that will take the query text and output a list of found parameters?

After researching the question, I haven't come up with anything that says you can get a list of parameters defined in the query text by using "this thing".

It may not be possible, and this is simply me wondering if it is.
Currently, after getting a list of parameters by crawling the query string, I have to ask the user to define the datatypes - and that could go away if it's possible to get a list automatically.

If you do not add a parameter to the SqlCommand that exists in the query string - you will get an exception saying something like

@ParameterName is defined in the query but no value was given

which is what makes me think it may be possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浸婚纱 2025-02-20 01:14:55

不在sqlclient中,因为它没有有关参数的数据类型信息。但是SQL Server可以从使用的列比较表达式中推断参数类型。

因此,请查看系统存储过程 sp_describe_undeclared_pa​​rameters 为您提供此信息。

Not in SqlClient, as it has no data type information about the parameters. But SQL Server can infer parameter types from the column comparison expressions they are used in.

So check out the system stored procedure sp_describe_undeclared_parameters to give you this information.

三生殊途 2025-02-20 01:14:55

通过执行此查询,您可以找到SP的参数列表

SELECT
param.parameter_id AS [ID],
param.name AS [Name]
FROM
sys.all_objects AS sp
INNER JOIN sys.all_parameters AS param ON param.object_id=sp.object_id
WHERE
(sp.type = 'P' OR sp.type = 'RF' OR sp.type='PC')and(sp.name='SP_NAME' and SCHEMA_NAME(sp.schema_id)='dbo')
ORDER BY
[ID] ASC

By executing this query, you can find the list of parameters of SP

SELECT
param.parameter_id AS [ID],
param.name AS [Name]
FROM
sys.all_objects AS sp
INNER JOIN sys.all_parameters AS param ON param.object_id=sp.object_id
WHERE
(sp.type = 'P' OR sp.type = 'RF' OR sp.type='PC')and(sp.name='SP_NAME' and SCHEMA_NAME(sp.schema_id)='dbo')
ORDER BY
[ID] ASC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文