我是 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。嗯...