используя bcp для экспорта данных в текстовые файлы с помощью c # - PullRequest
0 голосов
/ 06 января 2019

это мой .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;

Кто-нибудь знает, почему код не работает?

1 Ответ

0 голосов
/ 06 января 2019

Скорее всего, ваш файл создан под process.StartInfo.WorkingDirectory (который сейчас используется по умолчанию)

Вам нужно установить

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

, чтобы иметь возможность сохранить ваши результаты в заданном каталоге.

...