locked
How to add double quotes in a String.Format output

    Dotaz

  • hi all

    I am trying to execute the following command:

    cmd /c bcp "select * From Database1.dbo.Table1" queryout C:\table1.txt -w -t "^^" -r\r\n -T -S -E

    I am having difficulty in inserting the double quotes in my final string. Here is my function:

    		public static string BcpCommand(SqlExportElement elem)
    		{
    			string s;
    			s = String.Format("bcp {0} queryout {1} -w -t {2} -r{3} -T -S {4} -E",
    			elem.SqlCommand, Path.Combine(elem.OutputDirectory, elem.FileName), elem.ColumnDelimiter, elem.RowDelimiter, elem.SqlInstanceName);
    
    			return s;
    		}
    

     Any ideas? Thank you!


    JCD
    5. dubna 2011 16:55

Odpovědi

  •             this

    string a = "some text \"THIS IS IN QUOTATION\" some more text...";

                or this

    string b = @"some text ""THIS IS IN QUOTATION"" some more text...";

     

    so

    this

     

               s = String.Format("bcp {0} queryout {1} -w -t \"{2}\" -r{3} -T -S {4} -E",
                elem.SqlCommand, Path.Combine(elem.OutputDirectory, elem.FileName), elem.ColumnDelimiter, elem.RowDelimiter, elem.SqlInstanceName);



    or this

               s = String.Format(@"bcp {0} queryout {1} -w -t ""{2}"" -r{3} -T -S {4} -E",
                elem.SqlCommand, Path.Combine(elem.OutputDirectory, elem.FileName), elem.ColumnDelimiter, elem.RowDelimiter, elem.SqlInstanceName);

    • Označen jako odpověď Maximusdm 5. dubna 2011 18:42
    5. dubna 2011 17:37

Všechny reakce

  • Try this way:

    string a = "some text "\THIS IS IN QUOTATION"\ some more text...";
    


    Mitja
    • Navržen jako odpověď Ajith R Nair 5. dubna 2011 18:07
    • Zrušeno navržení jako odpověď Maximusdm 5. dubna 2011 18:42
    5. dubna 2011 16:57
  • You can insert double quotes by escaping them with a \:  For example:

     

    string test = "\"This is a test\"";  // Creates a string "This is a test"

     

    However, I would recommend using parameters instead of building your SQL statement as text.  This has many advantages, especially in terms of security.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    5. dubna 2011 17:25
  •             this

    string a = "some text \"THIS IS IN QUOTATION\" some more text...";

                or this

    string b = @"some text ""THIS IS IN QUOTATION"" some more text...";

     

    so

    this

     

               s = String.Format("bcp {0} queryout {1} -w -t \"{2}\" -r{3} -T -S {4} -E",
                elem.SqlCommand, Path.Combine(elem.OutputDirectory, elem.FileName), elem.ColumnDelimiter, elem.RowDelimiter, elem.SqlInstanceName);



    or this

               s = String.Format(@"bcp {0} queryout {1} -w -t ""{2}"" -r{3} -T -S {4} -E",
                elem.SqlCommand, Path.Combine(elem.OutputDirectory, elem.FileName), elem.ColumnDelimiter, elem.RowDelimiter, elem.SqlInstanceName);

    • Označen jako odpověď Maximusdm 5. dubna 2011 18:42
    5. dubna 2011 17:37
  • I am not calling BCP inside SQL so I won't be using SQL parameters ;-)

    Thank you


    JCD
    5. dubna 2011 18:45
  • If your using SQL server as Input you can use  select QUOTENAME(myfield,'"') as 'myfieldname'

    or try -t\",\" in your BCP command.

    6. března 2012 17:52
  • I think you can use single quotes on the command instead of double quotes.    This make the whole problem moot and easier to understand.

    Ta Ken

    7. března 2012 0:45