-1

I try to use SQL Server bcp utility in Visual C# like this:

command = new SqlCommand ("bcp "+ onlineConnect.Database+".dbo.T1 out "+ ConfigurationManager.AppSettings ["caleonline"]+"T1.dat -n -S " + onlineConnect.DataSource+" -U sa -P a" ,onlineConnect ); 

When I run it with breakpoints I get this:

CommandText 
"bcp ONLINE.dbo.T1 out H:\\ONLINE\\fisiere\\T1.dat -n -S ATTY\\SQLEXPRESS -U sa -P a"   

but I get an error:

System.Data.SqlClient.SqlException was unhandled Message=Incorrect syntax near '.'.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
atty
  • 1
  • 2
  • Have you tried enclosing the file path with quotation marks? – Markai Mar 26 '15 at 10:54
  • Calling `bcp` is **not** a SQL statement, therefore you cannot use `SqlCommand` for this. Just call the `bcp` utilty as an *external program* (using `Process.Start` or something like that) – marc_s Mar 26 '15 at 11:15
  • You are right, works fine with Process.Start – atty Mar 26 '15 at 11:57

1 Answers1

1

Prefix the command with exec xp_cmdShell

command=new SqlCommand ("exec xp_cmdShell 'bcp.exe'"+ onlineConnect.Database+".dbo.T1 out "+ ConfigurationManager.AppSettings ["caleonline"]+"T1.dat -n -S " + onlineConnect.DataSource+" -U sa -P a" ,onlineConnect );
Cristina Alboni
  • 1,014
  • 9
  • 15