C# 从SQL查询中获取参数名称
后端是PostgreSQL server 9.1 我正在尝试构建临时XML报告。报表文件将包含SQL查询,所有查询都必须以SELECT语句开头。SQL查询将具有参数。根据相关列的数据类型,这些参数将相应地呈现给用户以提供值 粗略的SQL查询:C# 从SQL查询中获取参数名称,c#,asp.net,regex,asp.net-4.0,C#,Asp.net,Regex,Asp.net 4.0,后端是PostgreSQL server 9.1 我正在尝试构建临时XML报告。报表文件将包含SQL查询,所有查询都必须以SELECT语句开头。SQL查询将具有参数。根据相关列的数据类型,这些参数将相应地呈现给用户以提供值 粗略的SQL查询: SELECT * FROM customers WHERE ( customers.customer_code=@customer_code AND customers.location=@location AND customers.t
SELECT * FROM customers
WHERE
(
customers.customer_code=@customer_code AND customers.location=@location
AND customers.type=
(
SELECT type from types
WHERE types.code=@type_code
AND types.is_active = @type_is_active
)
AND customers.account_open_date BETWEEN @start_date AND @end_date
)
OR customers.flagged = @flagged;
我想从查询字符串中获取列名和参数的列表,并将它们放入字符串数组中,稍后再进行处理
我只能使用以下正则表达式匹配参数:
@(?)(?<parameter>\w+)
如何立即匹配“@Parameter”、“=”和“BETWEEN”?我知道有点晚了,但为了将来的研究:
我认为这个正则表达式符合您的目的:
(\w+\.\w+(?:\s?\=\s?\@\w+|\sBETWEEN\s\@\w+\sAND\s\@\w+))
检查这里,并仔细阅读每个部分的解释
基本上,它首先查找customer.xxx_yyy
列,然后查找@variable1和@variable2之间的=@variable
或
捕获的组:
MATCH 1
1. [37-75]
`customers.customer_code=@customer_code`
MATCH 2
1. [80-108]
`customers.location=@location`
MATCH 3
1. [184-205]
`types.code=@type_code`
MATCH 4
1. [218-251]
`types.is_active = @type_is_active`
MATCH 5
1. [266-327]
`customers.account_open_date BETWEEN @start_date AND @end_date`
MATCH 6
1. [333-361]
`customers.flagged = @flagged`
如果您使用的是XML,那么为什么不使用parameters元素并省去麻烦呢?因此您希望在sql查询中找到“@{variablename}”,并用用户想要的实际值替换它?嘿,谢谢。:)难道你不认为SQL和XML的混合会变得非常复杂吗?你是对的@ganders。在这个SQL中有一个声明@parameters的存储过程吗?如果是,SqlCommandBuilder将解释参数。
MATCH 1
1. [37-75]
`customers.customer_code=@customer_code`
MATCH 2
1. [80-108]
`customers.location=@location`
MATCH 3
1. [184-205]
`types.code=@type_code`
MATCH 4
1. [218-251]
`types.is_active = @type_is_active`
MATCH 5
1. [266-327]
`customers.account_open_date BETWEEN @start_date AND @end_date`
MATCH 6
1. [333-361]
`customers.flagged = @flagged`