I want to run process in SQL Server CLR this my code:
[SqlProcedure]
private static int RunExecutable()
{
SqlDataRecord sqlDataRecord = new SqlDataRecord(new SqlMetaData("message", SqlDbType.NVarChar, 1L));
SqlContext.Pipe.SendResultsStart(sqlDataRecord);
int lineCount = 0;
Process process = new Process();
process.StartInfo.FileName = "ipconfig.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
sqlDataRecord.SetString(0, "OnDataReceived");
SqlContext.Pipe.SendResultsRow(sqlDataRecord);
if (!String.IsNullOrEmpty(e.Data))
{
lineCount++;
sqlDataRecord.SetString(0, "[" + lineCount + "]: " + e.Data);
SqlContext.Pipe.SendResultsRow(sqlDataRecord);
}
});
process.Start();
process.BeginOutputReadLine();
while (!process.HasExited)
{
sqlDataRecord.SetString(0, "process WaitForExit ");
SqlContext.Pipe.SendResultsRow(sqlDataRecord);
process.WaitForExit(300);
}
}
and ipconfig.exe runs (I see in results "process WaitForExit"), but the OutputDataReceived event is not triggered.
The assembly was created in SQL Server 2019 Enterprise with PERMISSION_SET = UNSAFE;. If I run the same code as the standard console application everything works fine