1
'<ad name="'+ am.name+ '" tag="C" cityid="'+cast(am.city_id as varchar) +'" 
 stateid="'+convert(varchar(11),cast(cm.state_id as varchar)) +'" capital="N"> 
 </ad>' as chkdata 

I have above statement in SQL query.

I want to add this to C# string

Unsucessful attempt

string sqlqry = "\'<ad name=""\'+ am.name_id + \'"" tag=""C"" 
cityid=""\'+cast(am.city_id as varchar) +\'"" 
stateid=""\'+convert(varchar(11),cast(cm.state_id as varchar)) +\'"" capital=""N"">
</ad>\' as chkdata ,";

Later I am doing this . I am building a query in string.

sqlcountcmd = new SqlCommand(finalsqlqry, sqlconn);

+1 to all guaranteed

4
  • Just remove the @ as the string isn't verbatim... Commented May 20, 2013 at 8:06
  • tried it but didn't work Commented May 20, 2013 at 8:08
  • 1
    it would be far preferable to simply select the columns you want, and let your application layer worry about xml Commented May 20, 2013 at 8:09
  • msdn.microsoft.com/en-us/library/ms187339(v=sql.100).aspx Commented May 20, 2013 at 8:11

2 Answers 2

2

presumably that entire thing is to be treated as sql - not a composite of sql and C#. Thus if we use a verbatim literal we only need to worry about the "s:

string s = @"'<ad name=""'+ am.name+ '"" tag=""C"" cityid=""'+cast(am.city_id as varchar) +'"" 
stateid=""'+convert(varchar(11),cast(cm.state_id as varchar)) +'"" capital=""N""> 
</ad>' as chkdata ,";

However! I strongly suggest not doing this. You should just return the columns from SQL, and let your app tier worry about xml/html etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes entire thing is to be treated as sql - not a composite of sql and C#
1

So, if I've understood correctly, you'll only need to escape the double quotes " and not single quotes '

string sqlqry = "'<ad name=\"'+ am.name_id + '\" tag=\"C\" " +
"sectordetailid=\"'+cast(am.sector_detail_id as varchar) +'\" " +
"stateid=\"'+convert(varchar(11),cast(cm.state_id as varchar)) +'\" capital=\"N\">" +
"</ad>' as chkdata";

You can use the verbatim character @ I'm sure, but I've not got Visual Studio in front of me to check.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.