How to add double quotes in a String.Format output

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

  • 2011년 4월 5일 화요일 오후 4:55
     
      코드 있음

    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

모든 응답

  • 2011년 4월 5일 화요일 오후 4:57
     
      코드 있음

    Try this way:

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


    Mitja
    • 답변으로 제안됨 Ajith R Nair 2011년 4월 5일 화요일 오후 6:07
    • 답변으로 제안 취소됨 Maximusdm 2011년 4월 5일 화요일 오후 6:42
    •  
  • 2011년 4월 5일 화요일 오후 5:25
    중재자
     
     제안된 답변

    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".
    • 답변으로 제안됨 Ajith R Nair 2011년 4월 5일 화요일 오후 6:07
    •  
  • 2011년 4월 5일 화요일 오후 5:37
     
     답변됨

                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);

    • 답변으로 표시됨 Maximusdm 2011년 4월 5일 화요일 오후 6:42
    •  
  • 2011년 4월 5일 화요일 오후 6:45
     
     

    I am not calling BCP inside SQL so I won't be using SQL parameters ;-)

    Thank you


    JCD
  • 2012년 3월 6일 화요일 오후 5:52
     
     제안된 답변

    If your using SQL server as Input you can use  select QUOTENAME(myfield,'"') as 'myfieldname'

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

    • 답변으로 제안됨 ncnyrangerfan 2012년 3월 6일 화요일 오후 5:54
    •  
  • 2012년 3월 7일 수요일 오전 12:45
     
     
    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