是否可以自动获取使用SQLClient在查询字符串中定义的参数列表
我正在研究一个C#项目,该项目进行参数化查询并保存它们 - 然后按时间表执行并通过电子邮件发送结果(如果返回任何记录)。
我正在使用system.data.sqlclient
命名空间来创建sqlConnection
,sqlCommand
用于包含查询和参数信息,以及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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不在sqlclient中,因为它没有有关参数的数据类型信息。但是SQL Server可以从使用的列比较表达式中推断参数类型。
因此,请查看系统存储过程 sp_describe_undeclared_parameters 为您提供此信息。
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.
通过执行此查询,您可以找到SP的参数列表
By executing this query, you can find the list of parameters of SP