<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'C# - executing bcp command on a database residing on the remote server' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'C# - executing bcp command on a database residing on the remote server' posted on the 'C#' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Mon, 17 Jun 2013 23:19:19 -0700</pubDate>
    <lastBuildDate>Mon, 17 Jun 2013 23:19:19 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>C# - executing bcp command on a database residing on the remote server</title>
      <link>http://www.programmersheaven.com/mb/csharp/420883/420883/c---executing-bcp-command-on-a-database-residing-on-the-remote-server/</link>
      <description>Hi guys,&lt;br /&gt;
&lt;br /&gt;
I have the following query that executes BCP on a database residing on the remote server&lt;br /&gt;
&lt;br /&gt;
EXEC XP_CMDSHELL 'bcp "SELECT [surname],'', ''+[forename] AS forename, [honours] AS honours, REPLACE(REPLACE(REPLACE([body_text], ''&amp;amp;amp;'', ''&amp;amp;''), ''..'', ''.''), '';.'', ''.'') + CHAR(10) AS body_text FROM [Doctors_Latest].[dbo].[vw_ks_xml_practitioners_ou
tput] AS doctor WHERE surname like ''A%'' or surname like ''*A%'' order by strSurnameSortkey, strForenameSortkey FOR XML PATH(''doctor''), ROOT(''root'')" queryout "F:\Test\MedicalPract_SURNAME_STARTS_WITH_A\Medica
l_Practitioners_Surname_Start_with_A_201115_112631
.xml" -c -C65001 -r -q -S ServerName -t \t -U username -P password '&lt;br /&gt;
&lt;br /&gt;
This query works fine if executed from within SQL Server Management Studio on the client machine but does not work from the code written in C# (residing on the client machine) :&lt;br /&gt;
&lt;br /&gt;
private void ExecuteQuery()&lt;br /&gt;
&lt;br /&gt;
        {&lt;br /&gt;
&lt;br /&gt;
            conn.Open();&lt;br /&gt;
&lt;br /&gt;
            SqlCommand cmd = new SqlCommand();&lt;br /&gt;
&lt;br /&gt;
            cmd.CommandTimeout = 5000;&lt;br /&gt;
&lt;br /&gt;
            cmd.Connection = conn;&lt;br /&gt;
&lt;br /&gt;
            cmd.CommandType = CommandType.Text;&lt;br /&gt;
&lt;br /&gt;
            cmd.CommandText = query;&lt;br /&gt;
&lt;br /&gt;
            cmd.StatementCompleted += new StatementCompletedEventHandler(cmd_StatementComple
ted);&lt;br /&gt;
&lt;br /&gt;
            OnTableProgressStart(this.message, queryCount);&lt;br /&gt;
&lt;br /&gt;
            int rows = cmd.ExecuteNonQuery();&lt;br /&gt;
&lt;br /&gt;
            queryCount++;&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
ExecuteNonQuery always returns 3&lt;br /&gt;
&lt;br /&gt;
Any idea please?&lt;br /&gt;
&lt;br /&gt;
I wonder why it works from within SQL Server Management Studio and not from a code written in C#&lt;br /&gt;
&lt;br /&gt;
Thanks!&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/csharp/420883/420883/c---executing-bcp-command-on-a-database-residing-on-the-remote-server/</guid>
      <pubDate>Wed, 12 Jan 2011 07:12:53 -0700</pubDate>
      <category>C#</category>
    </item>
  </channel>
</rss>