Converting :Pnnn to {nnn}

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

ADO net sql command contains parameters in form

:Pn

where n is 1-3 digits

How to convert this string to {n} format, i.e. remove :P and add curly
braces?

For example,

SELECT * FROM x WHERE :P1=1 AND :P99=3 should converted to


SELECT * FROM x WHERE {1}=1 AND {99}=3

Andrus.
 
How to convert this string to {n}  format, i.e.  remove :P and add curly
braces?

Something like this should do the trick:

value = Regex.Replace(value, @":P([0-9]{1,3})", @"{$1}");
 
Random,
value = Regex.Replace(value, @":P([0-9]{1,3})", @"{$1}");

Thank you very much. It works.
I need also to re-number all parameter numbers so that every parameter will
have sequential number starting at 0.
For example:

SELECT * FROM v WHERE :P4=a AND b=:P5 AND c=:P1

should converted to

SELECT * FROM v WHERE {0}=a AND b={1} AND c={2}

How to do this ?

Andrus.
 
I'm sorry previous message was a bit wrong.
I need to add same fixed offset to all parameter numbers when merging ADO
..NET command strings.

For example: command

SELECT * FROM v WHERE :P4=a AND b=:P5 AND c=:P1

If Offset is 100 then it should converted to

SELECT * FROM v WHERE :P104=a AND b=:P105 AND c=:P101

How to do this ?

Andrus.
 
You can specify a Match Evaluator to handle the results for you, for
instance:

value = Regex.Replace(value, @":P([0-9]{1,3})",
delegate(Match match)
{
int hit = int.Parse(match.Groups[1].Value);
hit += 100;
return "{" + hit.ToString() + "}";
});

Or:

int hit = -1;
value = Regex.Replace(value, @":P([0-9]{1,3})",
delegate(Match match)
{
hit++;
return "{" + hit.ToString() + "}";
});
 
Back
Top