我是编程C#的新手。我编写了一个查询oracle dB的脚本,如果rowcount是> 0,它应该运行一个bat文件,但如果不是,它应该结束。
这是我到目前为止编写的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client; // ODP.NET Oracle managed provider
using Oracle.DataAccess.Types;
namespace trigOra
{
class Program
{
static void Main(string[] args)
{
string oradb = "Data Source=OMP1;User Id=user;Password=pwd;";
OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT cast(Count(*) as varchar(20)) as trig FROM DMSN.DS3R_1XRTT_VOICE_TRIGGER";
//cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
int cnt;
if (int.TryParse(dr.GetString(0), out cnt))
{
if (cnt > 0)
{
System.Diagnostics.Process.Start(@"C:\AutoBatch\backup\trigger.bat");
cmd.CommandText = "TRUNCATE TABLE DMSN.DS3R_1XRTT_VOICE_TRIGGER";
cmd.ExecuteNonQuery();
}
}
conn.Dispose();
}
}
}
代码有效,除了它似乎总是执行这部分:
if (cnt > 0)
{
System.Diagnostics.Process.Start(@"C:\AutoBatch\backup\trigger.bat");
cmd.CommandText = "TRUNCATE TABLE DMSN.DS3R_1XRTT_VOICE_TRIGGER";
cmd.ExecuteNonQuery();
即使rowcount返回的是0.嗯......
答案 0 :(得分:1)
您的bat文件必须使用WaitForExit()函数运行:
int exitCode = RunProcessForExitCode(filename, arguments);
private static int RunProcessForExitCode(string processFilename, string processArguments)
{
//make process output possible to machine-reading
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = processFilename,
Arguments = processArguments,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
Console.Write(process.StandardOutput.ReadToEnd());
process.WaitForExit();
return process.ExitCode;
}