0

Okay. I'm at wit's end and haven't found a posting that addresses my problem completely. I'm new to C# and I'm unfamiliar with databases. What I do know is I must output a data file into a 'native' format (instead of ASCII which is why I assume I must use BCP) and when I run my code, no files are created (and when I do create a file before export, no data is exported to the file).

So, here is my C# code

SqlConnection myConn = new SqlConnection("server=localhost\\....;Trusted_Connection=yes;database=...;");
.....
string sp_config = "exec sp_configure 'show advanced options', 1";
command = new SqlCommand(sp_config, myConn);
command.ExecuteNonQuery();

sp_config = "reconfigure";
command = new SqlCommand(sp_config, myConn);
command.ExecuteNonQuery();

sp_config = "exec sp_configure 'xp_cmdShell', 1";
command = new SqlCommand(sp_config, myConn);
command.ExecuteNonQuery();

string bcp_command = "exec xp_cmdShell 'bcp REF_JDLM_DB.." +
                     table_name + " out " + m_destFolder + "\\" + table_name + "-n.dat -n -T'";
command = new SqlCommand(bcp_command, myConn);
command.ExecuteNonQuery();
myConn.Close();

Everything 'appears' to run find but none of the '-n.dat' files are exported. I'm open to any suggestions that allow me to generate files in the 'native' format.

Erik
  • 503
  • 1
  • 7
  • 26
  • Have you called `RECONFIGURE` after your sp_configure? – Filip De Vos Nov 12 '12 at 15:48
  • 1
    One obvious problem is the spelling mistake in your second `sp_configure` command. But assuming that's not it, I would start by saving the contents of the `bcp_command` string and then copy and paste it into SSMS to run it manually and see what happens. Using bcp.exe in this way is very prone to permissions errors and other problems, so it's very possible that even if it 'appears' to run it isn't. – Pondlife Nov 12 '12 at 18:35
  • @Pondlife: I've checked and you're right. Once I can get it to run, I'll post an answer. – Erik Nov 12 '12 at 20:22
  • In the end, my solution was to drop this approach and just run bcp from the command line using ProcessStartInfo in C#. – Erik Nov 13 '12 at 13:16

0 Answers0