Reputation: 1655
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?
Upvotes: 2
Views: 15519
Reputation: 150
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();
Upvotes: 6