2

I have this python code.

import pyodbc
import time

print("Hello")
plexString = "{call sproc164407_2053096_651466 ()}"
connectionPlex = pyodbc.connect('DSN=PlexReport32; UID=XXXX; PWD=XXX', autocommit = True)
cursorPlex = connectionPlex.cursor()

connectionLocal = pyodbc.connect("DRIVER={SQL Server}; SERVER=XXX; DATABASE=Plex; Trusted_Connection=yes; connection timeout=30")
cursorLocal = connectionLocal.cursor()

cursorPlex.execute(plexString)
rows = cursorPlex.fetchall()

for row in rows:
    date1 = row[1].rstrip("0")
    date2 = row[2].rstrip("0")
    row[1] = date1
    row[2] = date2
    cursorLocal.execute('INSERT INTO Regraded_Serials VALUES (?,?,?,?,?,?,?,?,?,?,?,?)', row)
    cursorLocal.commit()

It is saved as a file called PlexStoredProcedures.pyw in a folder. I have used pyw because I read this stops it from opening a console window. I use this python script to extract data from a remote database and add it to the local sql server. C# has issues with this that are beyond my control.

But I can't seem to just execute the script. I don't need to add any arguments, and I don't need it to return anything back. I just want it to run the script, and for C# to wait for it to finish before continuing. I have looked online but the answers to this simple question are always far from easy to understand. This is the best I have so far in C#. It is working, but it isn't running the script, or at least, the script is not grabbing and inserting the data, which it does if I run in manually.

C# code:

try
            {
                ProcessStartInfo pythonInfo = new ProcessStartInfo();
                Process python;
                pythonInfo.FileName = @"C:\Visual Studio Projects\PlexStoredProcedures\PlexStoredProcedures\PlexStoredProcedures.pyw";
                //pythonInfo.Arguments = string.Format("{0} {1}", cmd, args);
                pythonInfo.CreateNoWindow = false;
                pythonInfo.UseShellExecute = true;

                Console.WriteLine("Python Starting");
                python = Process.Start(pythonInfo);
                python.WaitForExit();
                python.Close();
                Console.WriteLine("Python Exiting");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

I know it is probably something really simple but I can't seem to find what It is that I need to do.

Also, I would love to run this in IronPython but apparently IronPython will not add the module pyodbc.

Any suggestions would be helpful, but more helpful would be also if you can show me how?

Danrex
  • 1,657
  • 4
  • 31
  • 44

1 Answers1

6

Have you tried executing python.exe with the script as an argument? It may be as simple as the script not properly executing on its own.

Like This:

ProcessStartInfo pythonInfo = new ProcessStartInfo();
Process python;
pythonInfo.FileName = @"C:\Python27\python.exe";
pythonInfo.Arguments = @"C:\Visual Studio Projects\PlexStoredProcedures\PlexStoredProcedures\PlexStoredProcedures.pyw";
pythonInfo.CreateNoWindow = false;
pythonInfo.UseShellExecute = true;

Console.WriteLine("Python Starting");
python = Process.Start(pythonInfo);
python.WaitForExit();
python.Close();
briansyph
  • 150
  • 11
  • I tried it but it's running thorugh the program with no exceptions but moves straight on and the python code doesn't seem to run. – Danrex Oct 08 '15 at 01:37
  • You get no exceptions thrown or wites to the console? By the way put that code inside your try catch. – briansyph Oct 08 '15 at 01:40
  • So I have changed the code so it is now 'opening' the py file and is definitely waiting for it to exit but nothing actually happens. Will add the new code to my file now. – Danrex Oct 08 '15 at 01:53
  • Try setting shellexecute to true or for now changing the python extension to .py instead of .pyw. This way you may actually be able to see some output. – briansyph Oct 08 '15 at 02:06
  • I have done both, and put a 'Hello World' in the python script which it prints out in the C# console because I have also added the streamreader. BUt it doesn't grab the data? – Danrex Oct 08 '15 at 02:35
  • To add it is even grabbing the data, just not entering it into SQL Server. – Danrex Oct 08 '15 at 02:43
  • I'm gonna mark this as correct since you did answer my original problem, and open a new question since it has now deviated somewhat from the topic. – Danrex Oct 08 '15 at 02:48
  • 1
    Well, egg on my face actually. For future reference it helps if your clearing of all data in the table you are inserting too is done prior to the pull from python not after. Long day what can I say. – Danrex Oct 08 '15 at 03:06