【问题标题】:using bcp to export data to text fie with c#使用 bcp 将数据导出到带有 c# 的文本文件
【发布时间】:2019-01-06 13:02:17
【问题描述】:

这是我的 .bat 文件,它在命令提示符下工作,但在我的 c# 代码中它不起作用: 这是.bat文件代码:

bcp "select * from %4.att.Attendance where Date <= '%5' " queryout  "Attendance.txt"  -c  -b  200000 -S %1 -U %2 -P %3 
bcp "select * from %4.att.PairedAttendnce where Date <= '%5' " queryout "PairedAttendnce.txt"  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.att.PersonDateStructure where Date <= '%5' " queryout "PersonDateStructure.txt"  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.att.dailyResult where Date <= '%5' " queryout "DailyResult.txt"  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.att.WPResult where WPID  <= %7  " queryout "WPResult.txt  "  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.att.BaseResultCredit where date <= '%5'" queryout "BaseResultCredit.txt"  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.att.Credit where StartDate <= '%5' " queryout "Credit.txt"  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.Wfo.nwfDoc where SDate  <= '%5'" queryout "nwfDoc.txt"  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.Sec.Eventlog where REPLACE(LEFT(CONVERT(VARCHAR, CreateDateTime, 120), 10), '-', '/') <= '%6' " queryout "Eventlog.txt"  -c -b 200000 -S %1 -U %2 -P %3 
bcp "select * from %4.Cms.DDLLog where  REPLACE(LEFT(CONVERT(VARCHAR,  PostTime, 120), 10), '-', '/')  <= '%6' " queryout "DDLLog.txt"  -c -b 200000 -S %1 -U %2 -P %3 

这是c#代码:

 string MyBatchFile = System.IO.Directory.GetCurrentDirectory() + @"\BatchFiles\Export.bat";

            var process = new System.Diagnostics.Process();

            DateTime dt =  PersianDate.ConvertDate.ToEn (dateOfArchive);

            process.StartInfo.FileName = MyBatchFile;
            // process.StartInfo.Arguments = String.Format("\"{0}\"", serverDBIP, ServerDbUserName, ServerDbPassword, ServerDBName, dateOfArchive, dt.ToShortDateString(), wpid);
            process.StartInfo.Arguments = String.Format("\"{0}\"\"{1}\"\"{2}\"\"{3}\"\"{4}\"\"{5}\"\"{6}\"",
            serverDBIP, ServerDbUserName, ServerDbPassword, ServerDBName, dateOfArchive,dt.ToShortDateString(), wpid);

            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            bool b = process.Start();
            Thread.Sleep(20000);
            //string errorMessage = process.StandardError.ReadToEnd();
            process.WaitForExit();

            //string outputMessage = process.StandardOutput.ReadToEnd();
            //process.WaitForExit();    
            process.Close();
            return b;

有人知道为什么代码不起作用吗?

【问题讨论】:

  • “不起作用”不是错误消息或问题陈述。请提供关于正在发生的事情的完整、清晰、详细的信息。
  • 我的意思是执行代码后没有创建文本文件
  • 您遇到错误了吗?一定有什么。我们无法像这样为您提供帮助.. 我们需要了解更多关于上下文的信息
  • 请编辑问题以包含您拥有的任何额外信息。我们需要的远不止这些。请阅读stackoverflow.com/help/how-to-ask 以了解您可能想要包含的信息类型,以及如何组织您的问题。我们想帮助你,但我们不能不知道更多。请记住:我们无法读取您的屏幕、磁盘……或您的思想!谢谢。
  • 不幸的是我没有收到任何错误或异常

标签: c# sql bcp


【解决方案1】:

您的文件很可能是在process.StartInfo.WorkingDirectory 下创建的(目前是默认设置)

你需要设置

process.StartInfo.WorkingDirectory = <your-working-dir>

为了能够将您的批处理结果保存在给定目录中。

【讨论】:

  • 当我在命令提示符下运行代码时,它可以工作并创建txt文件,目录是正确的
  • 天哪!您的应用程序不知道您的工作目录。你需要让你的应用程序知道你的 WorkingDirectory
  • 但是怎么做呢?我尝试了上面的代码,但它对我不起作用?请帮忙
  • 我应该写什么而不是 process.StartInfo.WorkingDirectory = ?我在这里定义了批处理文件地址:string MyBatchFile = System.IO.Directory.GetCurrentDirectory() + @"\BatchFiles\Export.bat";
  • 只需System.IO.Directory.GetCurrentDirectory(),然后在该目录下查看
猜你喜欢
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2014-02-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多