0

I've just written my first C# code ever! I followed this example in creating a data pull from Oracle:

Here is my current code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Oracle.DataAccess.Client; // ODP.NET Oracle managed provider
using Oracle.DataAccess.Types;

namespace OraTrigger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string oradb = "Data Source=server;User Id=user;Password=pass;";
            OracleConnection conn = new OracleConnection(oradb); // C#
            conn.Open();

            OracleCommand cmd = new OracleCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT cast(count(D_DTM) AS varchar(20)) as trig FROM DMSN.DS3R_FH_1XRTT_BTS_LVL_KPI"; 
            cmd.CommandType = CommandType.Text;

            OracleDataReader dr = cmd.ExecuteReader();
            dr.Read();
            label1.Text = dr.GetString(0);

            conn.Dispose();
        }
    }
}

How can I execute a batch file if the count is > 0? I tried to follow this. But a stupid question is, where does the location of the bat file go?

Community
  • 1
  • 1
lightweight
  • 3,227
  • 14
  • 79
  • 142
  • 1
    Your sample and most of the text seem to be absolutely not related to your question. Please consider editing and adding relevant code. I.e. so far real portion of the question seem to be one sentence and no code: "When to following this: *Executing Batch File in C#* where does the location of the bat file go?" – Alexei Levenkov Apr 10 '13 at 19:28
  • @JonSkeet - You are 100% correct. I will delete the comment. – Brian Apr 10 '13 at 19:29

2 Answers2

0

You could simply do this:

var startInfo = new ProcessStartInfo("{full path to .bat file");
Process.Start(startInfo);

and that would execute the batch file as if you double-clicked it in Windows. Now, the reason that works is because the property UseShellExecute is set to true by default on the ProcessStartInfo class. So, it's literally going to leverage the Windows shell to launch the file you provide it.

Another option is to just issue Start like this:

Process.Start("{full path to .bat file");

but the problem with this solution is you're not setup to send any parameters or switches to the application because you're not using ProcessStartInfo, but it may work for you.


So, the full code might look something like this:

int cnt;
if (int.TryParse(label1.Text, out cnt))
{
    if (cnt > 0)
    {
        var startInfo = new ProcessStartInfo("{full path to .bat file");
        Process.Start(startInfo);
    }
}
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • ok...I think I'm getting closer but for some reason, it fails at if(dr.GetInt16(0) > 0)....it tells me there is an "InvalidCastException"...is GetInt16 for integers? – lightweight Apr 10 '13 at 19:55
  • @user2061886, use `GetInt32`. – Mike Perrenoud Apr 10 '13 at 19:59
  • still getting that error tried this: if(dr.GetInt32(0) > 0) and this: if(dr.GetInt32(0) > ToInt32(0)) – lightweight Apr 10 '13 at 20:04
  • @user2061886, then that means the result you're getting from your query isn't an integer and can't easily be casted to one. I'd have to lookup the code behind `GetInt32` to see exactly what Microsoft is doing - but I'm sure they're keeping it pretty simple. You may have to resort to the solution I provided (have you tried that)? – Mike Perrenoud Apr 10 '13 at 20:06
  • seems like I'm still getting that...even casted my sql query result to INT cmd.CommandText = "SELECT cast(count(D_DTM) AS Int) as trig FROM DMSN.DS3R_FH_1XRTT_BTS_LVL_KPI"; cmd.CommandType = CommandType.Text; OracleDataReader dr = cmd.ExecuteReader(); dr.Read(); label1.Text = dr.ToString(); if(dr.GetInt32(0) > 0) – lightweight Apr 10 '13 at 20:10
  • Put a breakpoint on the `dr.GetInt32` line and start expanding the `dr` object and figure out what `Type` the value is. – Mike Perrenoud Apr 10 '13 at 20:17
  • how do I do that? just learning about c# today, or programming anything other than SQL today – lightweight Apr 10 '13 at 20:18
  • I also tried your above method but it put something funky in the text box..."Oracle.DataAccess.Client.OracleDataReader" is in label1 – lightweight Apr 10 '13 at 20:20
  • @user2061886 and that's a great indication that the value coming back from Oracle isn't really an integer. What was the text of the label. That's imperative to solving this problem. – Mike Perrenoud Apr 10 '13 at 20:22
  • no...it just came with this example...http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/GettingStartedNETVersion/GettingStartedNETVersion.htm I don't need the form, in the end, I want this to run every 15min (but was going to hold off on that until I had atleast this figured out). – lightweight Apr 10 '13 at 20:26
  • sorry...just understood your question....I don't understand how that is coming back as this is the query: cmd.CommandText = "SELECT cast(count(D_DTM) AS Int) as trig FROM DMSN.DS3R_FH_1XRTT_BTS_LVL_KPI"; cmd.CommandType = CommandType.Text; OracleDataReader dr = cmd.ExecuteReader(); – lightweight Apr 10 '13 at 20:28
0

Use Process.Start()

See the following:

http://www.dotnetperls.com/process-start

ScottTx
  • 1,483
  • 8
  • 12