C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C# - executing bcp command on a database residing on the remote server Posted by shigarr on 12 Jan 2011 at 7:12 AM
Hi guys,

I have the following query that executes BCP on a database residing on the remote server

EXEC XP_CMDSHELL 'bcp "SELECT [surname],'', ''+[forename] AS forename, [honours] AS honours, REPLACE(REPLACE(REPLACE([body_text], ''&'', ''&''), ''..'', ''.''), '';.'', ''.'') + CHAR(10) AS body_text FROM [Doctors_Latest].[dbo].[vw_ks_xml_practitioners_output] 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\Medical_Practitioners_Surname_Start_with_A_201115_112631.xml" -c -C65001 -r -q -S ServerName -t \t -U username -P password '

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

private void ExecuteQuery()

{

conn.Open();

SqlCommand cmd = new SqlCommand();

cmd.CommandTimeout = 5000;

cmd.Connection = conn;

cmd.CommandType = CommandType.Text;

cmd.CommandText = query;

cmd.StatementCompleted += new StatementCompletedEventHandler(cmd_StatementCompleted);

OnTableProgressStart(this.message, queryCount);

int rows = cmd.ExecuteNonQuery();

queryCount++;

}

ExecuteNonQuery always returns 3

Any idea please?

I wonder why it works from within SQL Server Management Studio and not from a code written in C#

Thanks!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.