Converting :Pnnn to {nnn}

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.
 
R

Random

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}");
 
A

Andrus

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.
 
A

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.
 
R

Random

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() + "}";
});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top