If you receive your string in a variable you should do interpolation by yourself
To do so, you need to find all placeholders and replace they value. You can do this with regular expression (can be simplified if placeholder can start with digits)
{(?<placeholder>[a-z_][a-z0-9_]*?)}
Next method will do this replacement:
public static string Replace(string input, Dictionary<string, object> replacement)
{
var regex = new Regex("{(?<placeholder>[a-z_][a-z0-9_]*?)}",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
return regex.Replace(input, m =>
{
var key = m.Groups["placeholder"].Value;
if (replacement.TryGetValue(key, out var value))
return value.ToString();
throw new Exception($"Unknown key {key}");
});
}
Usage:
var input = "Insert into Emp(Id,Name,Number,Address) values({Id},\"Abc\",{Number},{Address});";
var replaces = new Dictionary<string, object>
{
{"Id", 123},
{ "Number", 55668878},
{"Address", "\"test address\"" },
};
var result = Replace(input, replaces);
Console.WriteLine(result);
Ouput:
Insert into Emp(Id,Name,Number,Address) values(123,"Abc",55668878,"test address");