Substitution inside verbatim string literals?
Why not make use of string.Format? In the specific example you gave, you could do something like
string query = @"SELECT c.CUSTOMER_ID, COALESCE (c.FIRST_NAME, ''_ + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME ct.NAME as CUSTOMER_TYPEFROM {0} AS ct INNER JOIN {1} AS c ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID";
And the invoke
string real_query = string.Format(query, tblName1, tblName2);
Yes, you can use the String.Format method.
string custtype = "CT_CUSTOMER_TYPE";string cust = "CUSTOMER"; string query = @"SELECT c.CUSTOMER_ID, COALESCE (c.FIRST_NAME, ''_ + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME ct.NAME as CUSTOMER_TYPEFROM {0} AS ct INNER JOIN {1} AS c ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID"; string endQuery = String.Format(query, custtype, cust);
The String.Format method will replace the parameters identified by {x} sequentially with the arguments following the string, so {0} will be replaced with the value of custtype, and {1} with the value of cust.
You might consider using LINQ to SQL, if you haven't already.
Answering the actual question, you could use string.Format
as the others have mentioned if you're creating the query. If you wish to modify an existing string, try using string.Replace
or Regex.Replace
as described here. That is:
string query = @" SELECT c.CUSTOMER_ID, COALESCE (c.FIRST_NAME, ''_ + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME ct.NAME as CUSTOMER_TYPE FROM CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID ";query.Replace("CT_CUSTOMER_TYPE", "NEW_TABLE_NAME");