1

我是 C# 编程新手。我编写了一个查询 oracle dB 的脚本,如果行数 > 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();

即使返回的行数为 0。嗯...

4

1 回答 1

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;
    }
于 2013-04-20T11:28:18.390 回答