为什么我的bcp查询不起作用?

时间:2016-02-08 13:36:34

标签: c# bcp

我是C#的初学者,我想在C#中使用abc(get<T>(str)...);实用程序,为此,我写了这段代码:

bcp

该代码已运行,但我的 string connstr = "Data Source=192.168.50.172;Initial Catalog=CDRDB;User ID=CDRLOGIN;Password=beh1368421"; //string connstr = "Enter your connection string here"; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "bcp"; proc.StartInfo.Arguments = @"select * from CDRTABLE"" queryout c:\filename.csv -S 192.168.50.172 -U CDRLOGIN -P beh1368421 -f -w"; proc.Start(); 驱动器上未创建filename.csv。发生了什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

对于从Bcp执行的System.Diagnostics.Process(),应使用-c-CRAW选项。

以下是示例代码。

string execPath = @"..\bcp.exe";
string localFile = @"MetaData.Table";
string localPath = @"..\OutputFolder";

System.Diagnostics.Process P = new System.Diagnostics.Process();
P.StartInfo.UseShellExecute = false;
P.StartInfo.RedirectStandardOutput = true;
P.StartInfo.RedirectStandardError = true;
P.StartInfo.CreateNoWindow = true;
P.StartInfo.FileName = execPath;

P.StartInfo.Arguments = string.Concat("\"", "MetaData.Table", "\" out \"", localPath, "\\", localFile, ".dat\" -c -C RAW -t\"", separator, "\" -S \"", "(local)", "\" -T");

P.Start();
P.WaitForExit();
P.Close();
P.Dispose();

您还可以使用BULK INSERT Transact-SQL语句并使用ExecuteNonQuery的{​​{1}}方法在C#代码中使用BCP。

下面的内容会起作用:

SqlCommand
相关问题